My receiver class extends BroadcastReceiver, i want to when app screen on intent open MainActivity but it doesnt run effectively. sometimes works but mostly doesnt work intent, doesnt go to mainActivity why?? Actually android 9 version doesnt work effectively
@Override
public void onReceive(Context context, Intent intent) {
Log.e("ekran", "açıldı");
if(intent.getAction().equals("android.intent.action.SCREEN_ON") ){
wasScreenOn = true;
Toast.makeText(context," burda çalışyıon",Toast.LENGTH_SHORT).show();
Log.e("evet screen on","evet screen on");
Intent i = new Intent(context, MainActivity.class);
context.startActivity(i);
Toast.makeText(context," burda çalışyıon",Toast.LENGTH_SHORT).show();
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent =
PendingIntent.getActivity(context, 0, i, 0);
try {
pendingIntent.send();
Toast.makeText(context," burda çalışyıon",Toast.LENGTH_SHORT).show();
Log.e("pendingIntent","pendingIntent");
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
Log.e("printStackTrace","printStackTrace");
}
}
CodePudding user response:
please try set flags before start activity.
(i would write a comment but have not enough rep)
CodePudding user response:
Starting an activity when the screen goes on -- exactly the sort of thing that Android goes out of its way to prevent! Each version of Android gets more and more paranoid and strict about what you can or cannot do in the background. And starting an Activity from the background when the screen turns on is exactly one of those things that Android doesn't want you to do (and for good reason).
You may be able to do it if you create the PendingIntent
in your main activity while it is in the foreground. That's sort of the purpose of PendingIntent
-- to create an intent that you could reasonably execute in the foreground, and provide it to a background context that otherwise does not have the right to execute the plain intent. Creating it in the BroadcastReceiver
is wrong.
That being said, you're going to run into much bigger problems with BroadcastReceiver
, which is deprecated and partially crippled in Android 8, and becomes increasingly less functional in successive Android versions, until it becomes completely non-functional in Android 12 (maybe 10).
The replacement feature is JobScheduler
. Save yourself the heartbreak of finding out that you're going to have to completely rewrite that code to run on Android 12, and write for JobScheduler
now.
Not quite sure what you're trying to do. The Android Way for this sort of thing is to post a notification through NotificationManager
. Users can then click on your notification to bring your app to the foreground via a PendingIntent. The goal of the Android dev team as I understand it is to prevent ANY app from coming to the foreground without an explicit user action to make it do so (e.g. clicking on a notification). I'm pretty sure you're going to have problems bringing your app back to the foreground on Android 12 if it isn't done through NotificationManager
. And if there is a way, Google will probably remove it in Android 13. :-P
If you rummage through logcat carefully, you will find messages indicating why the PendingIntent
was rejected. But they are, admittedly, hard to find. And, frankly, it's quite likely that your BroadcastReceiver
just never gets called once your app goes into the background.
(lol. The more I write, the more I'm wondering whether you're trying to do something completely disreputable that you fortunately can no longer do on Android 9 . Bringing your app to the foreground after the screen comes on again sounds Decidedly Evil).