boot_completed not working on Android 10 Q API level 29

前端 未结 3 1998
别那么骄傲
别那么骄傲 2020-12-18 08:22

I need help.

I have an application that starts an Intent after the boot that works from Android 6 to Android 9 API level 28. But this code does not work on

3条回答
  •  忘掉有多难
    2020-12-18 09:13

    I know that this may be old but I have faced the same problem and according to this: https://developer.android.com/guide/components/activities/background-starts

    The easiest solution I came up with was simply adding

        
    

    And setting up the receiver:

    
            
                
            
        
    

    To the manifest.

    Receiver code:

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
    //            Intent n =  context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
     //            n.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
     //    Intent.FLAG_ACTIVITY_CLEAR_TASK);
     //            context.startActivity(n);
    
            Intent myIntent = new Intent(context, MainActivity.class);
            myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(myIntent);
        }
    }
    

    Both options work. The only downside I see is that it takes rather a while for app to load (can be up to 10 seconds from my testings)

    Leaving this here for other people if they encounter this as well. This only applies to android 10 and up.

    As stated by @legolas108, this requires drawing overlay, which can be done with:

    if (!Settings.canDrawOverlays(getApplicationContext())) {
                Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                Uri uri = Uri.fromParts("package", getPackageName(), null);
    
                myIntent.setData(uri);
                startActivityForResult(myIntent, REQUEST_OVERLAY_PERMISSIONS);
                return;
            }
    

提交回复
热议问题