當前位置:名人名言大全網 - 短信平臺 - 求助 關於Android廣播如何全局監聽

求助 關於Android廣播如何全局監聽

這個是onCreate裏面的代碼

// 註冊接收新消息的監聽廣播

msgReceiver = new NewMessageBroadcastReceiver();

IntentFilter intentFilter = new IntentFilter(EMChatManager

.getInstance().getNewMessageBroadcastAction());

intentFilter.setPriority(3);

registerReceiver(msgReceiver, intentFilter);

下面是自己寫的廣播裏的操作

private class NewMessageBroadcastReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

// 註銷廣播

abortBroadcast();

// 消息id(每條消息都會生成唯壹的壹個id,目前是SDK生成)

String msgId = intent.getStringExtra("msgid");

EMMessage message = EMChatManager.getInstance().getMessage(msgId);

String news = ((TextMessageBody) message.getBody()).getMessage();

// 發送人id

String userId = intent.getStringExtra("from");

username = userId;

Info info = new Info();

info.setText(news);

info.setId(msgId);

info.setUserid(userId);

list.add(info);

}

}