crunky pii
crunky pii

Reputation: 35

Android startActiviy in BroadcastReceiver

Originally, it received the BootComplete Action and tried to start automatically when the app completes booting. However, while checking because startActivity did not work, I found out that context.startActivity executed by the Action received from BoradcastReceiver does not work.

BroadcastReceiver

     Intent startIntent = new Intent(context.getApplicationContext(),MainActivity.class);
     startIntent.setFlags(FLAG_ACTIVITY_NEW_TASK);
     context.startActivity(startIntent);

It's a very simple code that I can't explain, but it gets Received, but the app doesn't start. The existing apps seem to work without problems. It feels like a ghost.

There is a log like this, but I don't know what the problem is.

D/BootReceiver: BootRecived
D/ZLA: Setting app side flag to false due to  ActivityStarter-Normal Launch

Upvotes: 0

Views: 59

Answers (2)

Amirhosein
Amirhosein

Reputation: 4446

Please add the following permission to manifest and ask for user permission once when the app opened the first time by calling startActivity(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)); somewhere in your app :

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

Upvotes: 1

snachmsm
snachmsm

Reputation: 19273

if you are targeting/running on Android 10 then it isn't possible to startActivity, thats Android new policy (check out HERE)

now docs suggest to show Notification and user may pick it and this will start your Activity (or may remove it and your app won't start)

Upvotes: 1

Related Questions