我正在編寫android messenger。當應用程序被最小化並且某些聯繫人寫入時,通知會成功拋出。如果寫入兩個聯繫人,則每個消息顯示在不同的框中。 Android如何從通知中放置和檢索數據
所以我需要把,然後讓接觸式ID ..
這裏是我在應用程序代碼,從那裏通知拋出:
private void showNotification(Message m) {
Contact temp = dbHelper.getContact(m.getPersonID());
Intent notificationIntent = new Intent(getApplicationContext(), ChatActivity.class);
notificationIntent.putExtra("prsID", m.getPersonID());
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification.Builder builder = new Notification.Builder(this);
builder.setTicker("Messenger: New Message");
builder.setContentTitle(temp.getName());
builder.setContentText(m.getMessage());
builder.setSmallIcon(R.drawable.ic_launcher);
if (temp.hasImage()){
Bitmap bmp = BitmapFactory.decodeByteArray(temp.getImageByteArr(), 0, temp.getImageByteArr().length);
builder.setLargeIcon(bmp);
}
Notification noti = builder.setContentIntent(pIntent).build();
noti.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(temp.getId(), noti);
}
這裏是代碼ChatActivity,在那裏我「M試圖檢索聯繫人ID:
@Override
protected void onResume() {
super.onResume();
if (this.guest != null){
int prsid = getIntent().getIntExtra("prsID", -1);
if (prsid != -1){
this.guest = this.messageLoader.getContact(prsid);
ContactForChat.getInstance().setContact(this.guest.getId());
} else {
this.guest = this.messageLoader.getContact(this.guest.getId());
ContactForChat.getInstance().setCurrentID(this.guest.getId());
}
this.guest.setNewMessage(false);
this.messageLoader.updateContact(guest);
}
if (this.messageLoader != null)
this.messageLoader.chatOnPause(false);
}
但INT prsid = getIntent()getIntExtra( 「prsID」,-1);。總是返回-1
有人能幫助我嗎?如何正確地做到這一點? 預先感謝您
UPDATE
其實,我猜getIntent()返回null:
SOLUTION
在ChatActivity我不得不重寫的方法(包括,a和ab有正確的值):
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Bundle a = intent.getExtras();
int ab = a.getInt("prsID");
int b = intent.getIntExtra("prsID", -1);
}
我找到了解決辦法:我不得不重寫:'保護無效onNewIntent(意向意圖)'和裏面的代碼看起來像:'super.onNewIntent(intent); \t \t Bundle a = intent.getExtras(); \t \t int ab = a.getInt(「prsID」); ' –