Tôi đang cố gắng gửi thông tin từ thông báo đến hoạt động được gọi, trong khi từ hoạt động của tôi, tôi đã bị vô hiệu.Thông báo Android PendingIntent Extras null
Mã cho thông báo là:
private void showNotification() {
Intent resultIntent = new Intent(this, MainActivity.class);
if (D)
Log.d(TAG, "Id: " + Id);
resultIntent.putExtra("ineedid", deviceId);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MeterActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
// Bundle tmp = resultIntent.getExtras();
// if (tmp == null) {
// Log.d(TAG, "tmp bundle is null");
// } else {
// long id = tmp.getLong("ineedid", -1);
// Log.d(TAG, "tmp id : " + id);
// }
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
BLEMessengerService.this)
.setSmallIcon(R.drawable.ic_action_search)
.setContentTitle("Event tracker")
.setContentText("Events received").setOngoing(true)
.setContentIntent(resultPendingIntent)
.setWhen(System.currentTimeMillis());
int mId = R.string.service_notification_start_service;
mNM.notify(mId, mBuilder.getNotification());
}
Mã cho thu thập thông tin từ ý định vào các hoạt động chính;
Bundle extras = getIntent().getExtras();
if (extras != null) {
long deviceID = getIntent().getLongExtra("ineedid",
-1);
if (ID == -1) {
if (D)
Log.i(TAG_D, "Wrong Id received.");
finish();
} else {
device = dataSource.getDeviceByID(deviceID);
if (D)
Log.i(TAG_D, "Get the id.");
}
} else {
if (D)
Log.d(TAG_D, "Bundle is null");
finish();
}
Tôi đã xác minh trước khi thông báo được thông báo, gói không phải là rỗng và có thêm id. Trong khi, khi tôi cố gắng lấy nó từ ý định, nó đã biến mất. Cứu giúp.
Tôi cũng đã cố gắng thêm "resultIntent.addFlags (Intent.FLAG_ACTIVITY_SINGLE_TOP);", nhưng không có tác phẩm. – antonio081014
Có một lý do bạn không sử dụng 'resultPendingIntent.getExtras()'? – PearsonArtPhoto
Tôi chỉ thực hiện những dòng đó để kiểm tra xem pendingIntent của tôi có Extras hay không, và tôi đã xác minh nó có những gì tôi đặt ở đó. – antonio081014