error : BroadcastReceiver trying to return result

匿名 (未验证) 提交于 2019-12-03 09:14:57

问题:

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> 

回答1:

Your onRecieve is altering the intent and trying to use it to start a service. You should use a new intent to start the service instead.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!