What I really want is, when the user has no connection, I want to show some dialog that he or she has no connection.
I tried to put this on my MainActivity but still didn’t work.
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo().isConnectedOrConnecting();
}
I also used this one but it didn’t also work:
public class ConnectionChangeReceiver extends BroadcastReceiver
{
@Override
public void onReceive( Context context, Intent intent )
{
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo( ConnectivityManager.TYPE_MOBILE );
if ( activeNetInfo != null )
{
Toast.makeText( context, "Active Network Type : " + activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
}
if( mobNetInfo != null )
{
Toast.makeText( context, "Mobile Network Type : " + mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
}
}
}
I added this on my manifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Can someone tell me what I’m doing wrong, I would really appreciate it a lot. Thanks in advance!
Maybe this link can help you a bit further: Broadcastreceiver to obtain ServiceState information
You could past the mentined CommunicationManager code in your activity, or better, if you need access from more activities, you could also declare the class as static in the Application Singletin instance and call it from there as described here
You also need to register the BroadcastReceiver in your application manifest:
Enjoy