My Broadcast receiver is working properly inside the Service until my application is running but immediately stops working if I close my application.
Here, I want my application to detect internet change and show a Toast every time but the Toast messages are shown until my application is working.
I've started my service in the MainActivity and defined my broadcast inside my service.
Inside onCreate in MainActivity-
Intent service = new Intent(MainActivity.this, LatestCallService.class);
startService(service);
BroadcastReceiver inside the service:
BroadcastReceiver br = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Inside", Toast.LENGTH_SHORT).show();
}
}
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
_mContext = getApplicationContext();
_mContext.registerReceiver(br, filter);
Please let me know what is wrong with my code.