boot_completed not working on Android 10 Q API level 29

前端 未结 3 1999
别那么骄傲
别那么骄傲 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:25

    context.startActivity() is not launching, I solved it the following way:

      private void restartApp( Context mContext) {
        try {
            long restartTime = 1000*5;
            Intent intents = mContext.getPackageManager().getLaunchIntentForPackage(mContext.getPackageName());
            PendingIntent restartIntent = PendingIntent.getActivity(mContext, 0, intents, PendingIntent.FLAG_ONE_SHOT);
            AlarmManager mgr = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                mgr.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + restartTime, restartIntent);
    
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                mgr.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + restartTime, restartIntent);
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }
    

提交回复
热议问题