Fix ServerService/Foreground-Service for Android 13+

This commit is contained in:
Xcreen
2024-11-13 19:14:42 +01:00
parent 8c63dc6c73
commit fbc0ed63a1
3 changed files with 17 additions and 1 deletions

View File

@@ -8,6 +8,8 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<application
android:name=".AppContext"
@@ -28,7 +30,9 @@
</intent-filter>
</activity>
<service android:name=".server.ServerService" />
<service android:name=".server.ServerService"
android:exported="false"
android:foregroundServiceType="dataSync" />
</application>
</manifest>

View File

@@ -25,6 +25,7 @@ class HomeFragment : Fragment() {
private var toggleServerBtn: Button? = null
private var appContext: AppContext? = null
private val SMS_PERMISSION_REQUEST = 100
private val NOTIFICATION_PERMISSION_REQUEST = 101
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.fragment_home, container, false)
@@ -42,6 +43,16 @@ class HomeFragment : Fragment() {
ActivityCompat.requestPermissions(requireActivity(), arrayOf(Manifest.permission.SEND_SMS), SMS_PERMISSION_REQUEST)
return@setOnClickListener
}
// Check if Notification-Permission is grant
if (Build.VERSION.SDK_INT >= 33) {
if (ActivityCompat.checkSelfPermission(v.context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
//Show Error Toast
Toast.makeText(v.context, resources.getText(R.string.no_notification_permission), Toast.LENGTH_LONG).show()
//Request Permission
ActivityCompat.requestPermissions(requireActivity(), arrayOf(Manifest.permission.POST_NOTIFICATIONS), NOTIFICATION_PERMISSION_REQUEST)
return@setOnClickListener
}
}
//Check if Device has a Sim-Card
try {
val telephonyManager = v.context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager

View File

@@ -15,6 +15,7 @@
<string name="server_failed_bindex">Server can not run on this port! (Bind-Exception)</string>
<string name="invalid_sim">Sim-Card is missing or not ready to send sms!</string>
<string name="no_sms_permission">SMS-Permission is required to run the server!</string>
<string name="no_notification_permission">Notification-Permission is required to run the server as a foreground-service!</string>
<string name="notification_text">Server is running! On port: %1$d</string>