BroadcastReceiver未接收类别意图

时间:2012-06-23 04:58:13

标签: android broadcastreceiver categories

我有以下代码,如果我的接收器使用intentFilter进行特定操作,则效果很好,但是如果我的接收器等待整个类别,则无效。我在网上尝试过一些例子,不知道为什么它不起作用,我也尝试删除动作参考,只留下类别......可以有人看看吗?

我的活动:

public class Receiver1Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    myReceiver = new MyReceiver();
    IntentFilter filter = new IntentFilter();
    filter.addCategory(MyService.MY_CATEGORY);
    registerReceiver(myReceiver, filter);
}

@Override
protected void onStart() {
    super.onStart();
    Log.i("activity", "onStart");
    Intent intent = new Intent(this, MyService.class);
    startService(intent);
}

@Override
protected void onPause() {
    super.onPause();
    Log.i("activity", "onPause");
    unregisterReceiver(myReceiver);
}

private MyReceiver myReceiver;
}

我的服务:

public class MyService extends Service {

public static final String MY_CATEGORY = "com.receiver1.mycategory";
public static final String MY_ACTION = "com.receiver1.myaction";

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Intent intent2 = new Intent(MY_ACTION);
    intent2.addCategory(MY_CATEGORY);
    sendBroadcast(intent2);
    return START_NOT_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

}

我的接收者:

public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
    Log.i("MyReceiver", "onreceive");        
}
}

我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.receiver1"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".Receiver1Activity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".MyService">
        <!-- 
        <intent-filter >
            <action android:name="com.receiver1.myaction2"/>
        </intent-filter>
         -->
    </service>
</application>

</manifest>

2 个答案:

答案 0 :(得分:1)

也把它放在你的活动课上希望它对你有帮助

 @Override protected void onResume() {     
  super.onResume();     
  registerReceiver(myReceiver,filter); 
 }

试试这个..

答案 1 :(得分:0)

使用

 myReceiver = new MyReceiver();
    IntentFilter filter = new IntentFilter(MyService.MY_ACTION);
    filter.addCategory(MyService.MY_CATEGORY);
    registerReceiver(myReceiver, filter);

而不是

myReceiver = new MyReceiver();
IntentFilter filter = new IntentFilter();
filter.addCategory(MyService.MY_CATEGORY);
registerReceiver(myReceiver, filter);

您缺少操作注册接收器时