0

I can not start activity in BroadcastReceiver while app not running (Just on real device, it still working well in virtual device, even I exit an app). Did I need any user permission

Intent intent1 = new Intent(MainActivity.this,AlarmReceiver.class);
intent1.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,2000000,intent1,PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND,5);
        alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent);
public class AlarmReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {

Intent intent1 = new Intent(context,Alarm_Activity.class);
                intent1.setFlags(FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent1);
}
}
NGK
  • 81
  • 1
  • 8
  • Which Android version is the real device running? Restrictions were introduced in Android 10 that disallow apps from starting Activities from the background, unless certain conditions are met: https://developer.android.com/guide/components/activities/background-starts. – Mike M. Jun 13 '20 at 07:38
  • My phone is running android 9 – NGK Jun 13 '20 at 11:12
  • Which manufacturer? – Mike M. Jun 13 '20 at 11:17
  • red mi note 6 pro. But did you have any idea to make it right in any common phone ? – NGK Jun 14 '20 at 04:06
  • On a normal system, that should still work on Android 9. However, those devices often have additional restrictions that prevent most 3rd-party apps from doing certain things – e.g., starting at boot, running in the background, etc. – without the user having explicitly allowed it. You'll need to locate those settings, and make sure your app is allowed. I've never used such a device, so I can't say where to go specifically, but check these posts: https://stackoverflow.com/q/30748107, https://stackoverflow.com/q/37927565, https://stackoverflow.com/q/48166206, https://stackoverflow.com/q/55258440. – Mike M. Jun 14 '20 at 04:19
  • Thank you very very very very much, it working now. – NGK Jun 14 '20 at 05:02
  • No problem! Glad you got it working. Cheers! – Mike M. Jun 14 '20 at 05:04

0 Answers0