Can't start activity from BroadcastReceiver on android 10

前端 未结 3 1436
长发绾君心
长发绾君心 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:52

    You can use SYSTEM_ALERT_WINDOW to force launch activity window in android 10, refer to this settingsuperposition setting:

    
    
    
                
                
              
      
        
            
                
                
            
        
    

    in launched app check permissions:

    private void RequestPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(this)) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                        Uri.parse("package:" + this.getPackageName()));
                startActivityForResult(intent, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);
            } else {
                //Permission Granted-System will work
            }
        }
    }
    

    you will can user intent as android older versions

    public class OnBootReceiver extends BroadcastReceiver {
        private static final String TAG = OnBootReceiver.class.getSimpleName();
    
        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                Intent activity = new Intent(context, MainActivity.class);
                activity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(activity);
            } catch (Exception e){
                Log.d(TAG,e.getMessage()+"");
            }
        }
    }
    

提交回复
热议问题