Search code examples
androidbroadcastreceiver

Activity can't start from broadcast receiver


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) {

                    }
                } 

Solution

  • 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