How do I start my app on startup?

前端 未结 8 1784
失恋的感觉
失恋的感觉 2020-11-22 01:06

I tried using the sample code in this link but it seems outdated and it did not work. So what changes do I have to make and to what files to have my app start automatically

8条回答
  •  遇见更好的自我
    2020-11-22 01:56

    For Android 10 there is background restrictions.

    For android 10 and all version of android follow this steps to start an app after a restart or turn on mobile

    Add this two permission in Android Manifest

        
        
    

    Add this in your application tag

    
            
                
                
            
    
    

    Add this class to start activity when boot up

    public class BootReciever extends BroadcastReceiver {
    
    @Override
    public void onReceive(Context context, Intent intent) {
    
        if (Objects.equals(intent.getAction(), Intent.ACTION_BOOT_COMPLETED)) {
            Intent i = new Intent(context, SplashActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
     }}
    

    We need Draw overlay permission for android 10

    so add this in your first activity

     private fun requestPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(this)) {
                val intent = Intent(
                    Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + this.packageName)
                )
                startActivityForResult(intent, 232)
            } else {
                //Permission Granted-System will work
            }
        }
    }
    

提交回复
热议问题