BroadCast onReceiver() not calling on Marshmallow and above SDK's

78 views Asked by At

I want to start services from Broadcast Receiver but Marshmallow and above SDK's will not callback. BOOT_COMPLETED is working fine with app foreground & background but CONNECTIVITY_CHANGE is not working on background .

My requirement is, i want to get callback when app with app foreground & background.

My manifest :

 <receiver
        android:name=".receivers.MessageBroadCastReceiver"
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>

My Broadcast Receiver:

public class MessageBroadCastReceiver extends BroadcastReceiver {
private static final String TAG = "MessageBroadCastReceive";
private Context ctx;

@Override
public void onReceive(Context context, Intent intent) {
    Log.d(TAG, "AppBroad " + intent.getAction());
    this.ctx = context;
    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            RestartMessageService();

    } else if (intent.getAction().equals("android.net.conn.CONNECTIVITY_CHANGE")) {
            RestartMessageService();

    }
}
}

am tired to registered the Broadcast actions in my main activity its working fine on foreground, once i close my app it will not call (Marshmallow and above SDK's).

Registered code:

    private void RegisterReceivers() {
    try {
        MessageBroadCastReceiver broadCastReceiver = new MessageBroadCastReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        getBaseContext().registerReceiver(broadCastReceiver, intentFilter);
        PrefManager.getInstance(this).setBroadcastReg(true);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }
}

Please give me solution.

Advance thanks...

0

There are 0 answers