我的广播接收器有问题,我不知道我做错了什么。
我已经阅读了很多帖子@stackoverflow,但没有任何帮助。
我正在从我的助手类发送广播,广播接收器在活动中实现。在发送之前,我在 helperclass 的 Intent 上添加了额外内容,但意图@活动有 null 额外内容。我究竟做错了什么?
这是我的源代码:
//帮助类
...
public static String BROADCAST_ACTION_DM = "de.je.toctohk.DISPLAYMESSAGE";
...
public void method() {
String msg = _intent.getStringExtra("message");
Intent broadcast = new Intent();
broadcast.setAction(BROADCAST_ACTION_DM);
broadcast.putExtra("msg", _chatentryid);
sendStickyBroadcast(broadcast);
}
//活动
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String test = getIntent().getStringExtra("msg");
Toast.makeText(getApplicationContext(), intent.getExtras().getString("msg"), Toast.LENGTH_SHORT).show();
}
};
protected void onResume() {
IntentFilter filter = new IntentFilter();
filter.addAction(GcmIntentService.BROADCAST_ACTION_DM);
_testintent = registerReceiver(receiver, filter);
super.onResume();
}
//显现
<activity
android:name="de.je.toctohk.activities.ChatActivity"
android:label="@string/title_activity_push" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="de.je.toctohk.DISPLAYMESSAGE" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
我已经尝试过的:
正常广播
仅在 Activity 中注册过滤器
仅在 Manifest 中注册过滤器
粘性广播
context.getIntent().getExtra…</p>
intent.getExtra…</p>
如果有人能给我一些建议,那就太好了。
非常感谢!!