I am trying to start an activity from the Android Jobservice and it was not starting. When I start an activity from the notification builder with addAction method I am able to do it but the same I have to do it without any user consent and here I am fail.
final Intent intent1 = Intent intent = new Intent(context, onActivity.class);
final PendingIntent pendingIntent = PendingIntent.getActivity(this, "1",
intent1 , PendingIntent.FLAG_ONE_SHOT);
if(acceptvalid) {
mBuilder.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.on))
.setSmallIcon(R.drawable.icon)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setAutoCancel(true)
.addAction(R.drawable.ic_yes, getString(android.R.string.yes), installPendingIntent);
notificationManager.notify(1, mBuilder.build());
} else {
try{
installPendingIntent.send();
} catch (PendingIntent.CanceledException e) {
LOG.error("error starting activity.", e);
}
}
The above if loop code works for me and else is not working. I even tried the below code in else loop:
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent1 );
What is my error?
From Android 10 background apps are not allowed to start activities directly. Instead they need to show notification to user and get user consent. An exception to this is If your app has SYSTEM_ALERT_WINDOW permission granted.
See
https://developer.android.com/guide/components/activities/background-starts