Can't start activity from BroadcastReceiver on android 10

前端 未结 3 1438
长发绾君心
长发绾君心 2020-12-05 21:06

I updated my OS version to android 10 last night, and since then the startActivity function inside the broadcast receiver is doing nothing. This is how I try to start the ac

3条回答
  •  误落风尘
    2020-12-05 21:49

    Android 10's restriction on background activity starts was announced about six months ago. You can read more about it in the documentation.

    So you need to have a high-level notification and when the user clicks on the notification your activity will be opened notifications

    public class UIExampleReceiver extends BroadcastReceiver {
    
    public static final String TAG_NOTIFICATION = "NOTIFICATION_MESSAGE";
    public static final String CHANNEL_ID = "channel_1111";
    public static final int NOTIFICATION_ID = 111111;
    private static final String TAG = "Receiver";
    
    @Override
    public void onReceive(Context context, Intent intent) {
    
        try {
    
                // If android 10 or higher
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P)
                {
    
                     startActivityNotification(context,NOTIFICATION_ID,context.getResources().getString(R.string.open_app), context.getResources().getString(R.string.click_app));
    
                }
                else
                {
                    // If lower than Android 10, we use the normal method ever.
                    Intent activity = new Intent(context, ExampleActivity.class);
                    activity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(activity);
                }
    
        } catch (Exception e)
        {
            Log.d(TAG,e.getMessage()+"");
        }
    }
    
    
     // notification method to support opening activities on Android 10
    public static void startActivityNotification(Context context, int notificationID, 
    String title, String message) {
    
        NotificationManager mNotificationManager =
                (NotificationManager) 
       context.getSystemService(Context.NOTIFICATION_SERVICE);
        //Create GPSNotification builder
        NotificationCompat.Builder mBuilder;
    
        //Initialise ContentIntent
        Intent ContentIntent = new Intent(context, ExampleActivity.class);
        ContentIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
        Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent ContentPendingIntent = PendingIntent.getActivity(context,
                0,
                ContentIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
    
        mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle(title)
                .setContentText(message)
                .setColor(context.getResources().getColor(R.color.colorPrimaryDark))
                .setAutoCancel(true)
                .setContentIntent(ContentPendingIntent)
                .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);
    
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID,
                    "Activity Opening Notification",
                    NotificationManager.IMPORTANCE_HIGH);
            mChannel.enableLights(true);
            mChannel.enableVibration(true);
            mChannel.setDescription("Activity opening notification");
    
            mBuilder.setChannelId(CHANNEL_ID);
    
       Objects.requireNonNull(mNotificationManager).createNotificationChannel(mChannel);
        }
    
     Objects.requireNonNull(mNotificationManager).notify(TAG_NOTIFICATION,notificationID, 
     mBuilder.build());
        }
    
    }
    

提交回复
热议问题