This is the 16th day of my participation in the August Text Challenge.More challenges in August

background

The project used to run under android7.0, listening for the system to start broadcasting, and then pulling up your activity. However, after Android 10, the method failed.

Android 10 (API level 29) and later have restrictions on background application startup. In Android10, when the App’s Activity is not in the foreground, it will be blocked by the system to start the Activity.

See: Google: Restrictions on launching activities from the background

Here are some summaries:

Android 10 (API level 29) and later impose limits on the amount of time background apps can start activities. These limits help minimize disruptions to users and give them more control over what is displayed on their screens. \

Note: To start the Activity, the system still treats the application running the foreground service as a “background” application.

Plan a

There are two solutions to this problem: first, use NotificationManager + full-screen Intent and add permissions:

//AndroidManifest <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />Copy the code
Intent fullScreenIntent = new Intent(this, CallActivity.class); PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("Incoming call") .setContentText("(919) 555-1234) "3 rows. / / the following as the key setPriority (NotificationCompat. PRIORITY_HIGH). SetCategory (NotificationCompat. CATEGORY_CALL) .setFullScreenIntent(fullScreenPendingIntent, true); NotificationManager notifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); notifyManager.notify(notifyId, builder.build());Copy the code

Scheme 2

Solution 2: dynamically register broadcast and add ACTION_MANAGE_OVERLAY_PERMISSION:

/ / in the AndroidManifest < USES - permission android: name = "android. Permission. SYSTEM_ALERT_WINDOW" / >Copy the code
if (! Intents = new intents (intents.ACTION_MANAGE_OVERLAY_PERMISSION); intent.setData(Uri.parse("package:" + getPackageName())); startActivityForResult(intent, 0); } m_adbReceiver = new AdbBootBroadcastReceiver(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("android.intent.action.BROADCAST_FORM_ADB"); registerReceiver(m_bootReceiver, intentFilter2);Copy the code

Among them, the canDrawOverlays method is a new one added to Android M(23) to check whether or not you currently have permission to “appear on other apps.” In system versions prior to 6.0, hover window permission is enabled by default and can be used directly.