Reputation: 45
Start Activity from Broadcast receiver is not working on android 9 but its working below android 9 it's working fine, I searched a lot regarding this but could not find the suitable solution. Does anyone face the same problem, here is my code .
public void onReceive(final Context context, Intent intent) {
try {
this.tm = (TelephonyManager) context.getSystemService("phone");
this.tm.listen(new PhoneStateListener() {
public void onCallStateChanged(int state, final String num) {
if (state == 1 && Receiver.this.preferences.getInt("start", 0) == 1) {
try {
new Handler().postDelayed(new Runnable() {
public void run() {
Receiver.this.i = new Intent(context, MainActivity.class);
context.startActivity(Receiver.this.i);
}
}, 300);
} catch (Exception e) {
}
}
Upvotes: 4
Views: 3538
Reputation: 4276
Add this in your AndroidManifest.xml
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Ask for display overlay permission from appropriate activity:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(! Settings.canDrawOverlays(context)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + context.getPackageName()));
startActivityForResult(intent, 101);
}
}
Handle it inside onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 101) {
if(!Settings.canDrawOverlays(context)) {
Toast.makeText(context, "This feature is crucial to this app", Toast.LENGTH_SHORT).show();
}
}
}
In your BroadcastReceiver implementation:
@Override
public void onReceive(Context context, Intent intent) {
Intent intent1 = new Intent(context, ToBeOpenedActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
}
Upvotes: 6
Reputation: 49
Android Q (API level 29) and higher place restrictions on when apps can start activities when the app is running in the background.
https://developer.android.com/guide/components/activities/background-starts
However, you can achieve this by giving alert window permission.
How to start a activity from a broadcast receiver when the screen is locked in Android Q
It is working like a charm
Upvotes: 2
Reputation: 5375
I see that you're listening to onCallStateChanged. But I think you need to ask permission for that on Android 9: https://stackoverflow.com/a/52025013/2212770
Upvotes: -1
Reputation: 331
Below I send on example for starting activity from the broadcast receiver, But this one is for starting the MainActivity after device reboot.
public class StartReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action= intent.getAction();
if( action.equals("android.intent.action.BOOT_COMPLETED") ){
Intent i= new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
Upvotes: 0
Reputation: 923
you need to add flag to the intent
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
before you call context.startActivity(i);
Upvotes: 2