错误:广播接收器试图返回结果 [英] error : BroadcastReceiver trying to return result

查看:159
本文介绍了错误:广播接收器试图返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,当设备连接到互联网来了解。

但我得到这个错误:

  20 03-12:35:06.801:E /广播接收器(28892):广播接收器尝试一种无序播放过程中返回结果


我的广播接收器:

 公共类ConnectivityChangeReceiver扩展广播接收器{
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
    //明确指定该服务类将处理这个意图。
    组件名补偿=新的组件名(context.getPackageName()
            YourService.class.getName());    intent.putExtra(isNetworkConnected,isConnected(上下文));
    context.startService((intent.setComponent(化合物)));
    的setResult code(Activity.RESULT_OK);
}公共布尔isConnected(上下文的背景下){
       ConnectivityManager connectivityManager =((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE));
       的NetworkInfo NETWORKINFO = connectivityManager.getActiveNetworkInfo();
       返回NETWORKINFO = NULL&放大器;!&安培; networkInfo.isAvailable()及&放大器; networkInfo.isConnected();
 }}


我的服务:

 公共类YourService扩展IntentService {公共YourService(){
    超(测试);
    // TODO自动生成构造函数存根
}@覆盖
保护无效onHandleIntent(意向意图){
  捆绑额外= intent.getExtras();
  布尔isNetworkConnected = extras.getBoolean(isNetworkConnected);
  //你的code
  如果(isNetworkConnected){
      Log.e(TAG,是);
  }}}


我的清单:

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
包=com.example.ex80
安卓版code =1
机器人:=的versionName1.0><用途-SDK
    安卓的minSdkVersion =11
    机器人:targetSdkVersion =18/>
<使用许可权的android:NAME =android.permission.INTERNET对/>
<使用许可权的android:NAME =android.permission.ACCESS_NETWORK_STATE/>
<应用
    机器人:allowBackup =真
    机器人:图标=@绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME
    机器人:主题=@风格/ AppTheme>
    <活动
        机器人:名字=com.example.ex80.MainActivity
        机器人:标签=@字符串/ APP_NAME>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.MAIN/>            <类机器人:名字=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
<接收机器人:名字=。ConnectivityChangeReceiver>
&所述;意图滤光器>
    <作用机器人:名字=android.net.conn.CONNECTIVITY_CHANGE/>
&所述; /意图滤光器>
< /接收器>
< /用途>< /清单>


解决方案

您onRecieve正在改变的意图,并试图用它来启动服务。你应该用一个新的意图来启动服务,而不是

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>

解决方案

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.

这篇关于错误:广播接收器试图返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆