I am trying to understand when device is connected to internet.
but I get this error :
03-12 20:35:06.801: E/BroadcastReceiver(28892): BroadcastReceiver trying to return result during a non-ordered broadcast My BroadcastReceiver :
public class ConnectivityChangeReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // Explicitly specify that which service class will handle the intent. ComponentName comp = new ComponentName(context.getPackageName(), YourService.class.getName()); intent.putExtra("isNetworkConnected",isConnected(context)); context.startService((intent.setComponent(comp))); setResultCode(Activity.RESULT_OK); } public boolean isConnected(Context context) { ConnectivityManager connectivityManager = ((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE)); NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected(); } } My service :
public class YourService extends IntentService{ public YourService() { super("Test"); // TODO Auto-generated constructor stub } @Override protected void onHandleIntent(Intent intent) { Bundle extras = intent.getExtras(); boolean isNetworkConnected = extras.getBoolean("isNetworkConnected"); // your code if(isNetworkConnected){ Log.e("TAG", "Yes"); } } } My manifest :
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.ex80" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.ex80.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".ConnectivityChangeReceiver" > <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver> </application> </manifest>