Listen to internet availability in service in android
I already create receiver to test network availability so I can receive when the android has or no network, but how can we listen to internet availability?
The goal. When log in fails (means no internet connection) we have to resend our log in data in Thread in service backgro开发者_StackOverflow中文版und task )
This should do the trick :
ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if( conMgr.getActiveNetworkInfo().isConnectedOrConnecting())
{
// your stuff
}
Remeber to put
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
精彩评论