I'm trying to check internet connectivity on recieving sms. But my service is force closing while changing internet state. E.g If I've enabled wifi its working fine but when I disable wifi app becomes closed with error. I'm using this code in onReceive function. Here is the code.
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if(isConnected == true) {
Toast.makeText(context, "Internet", Toast.LENGTH_LONG).show();
}
else if(isConnected == false){
Toast.makeText(context, "No Internet", Toast.LENGTH_LONG).show();
}