我正在開發可創建多個警報的應用程序,並且這些警報會向用戶發送通知,以便他們可以點擊通知並導致一個活動,其中包含警報詳細信息想顯示取決於臨時演員的價值,但我的演員總是空的,我不明白是什麼錯誤..我已經搜索了很多的問題,似乎沒有什麼能解決我的問題。帶有額外功能的Android通知意圖始終爲空
這是每個創建一個具有唯一標識的警報的地方。 「item.getID()」
NewAlarm.java
firstTime = System.currentTimeMillis();
intervalTime = (1000 * 60 * 4); // 4 minutes
// crear el pending intent y la alarma
Intent intent = new Intent(this, AlarmBroadcastReceiver.class);
intent.putExtra("ID", item.getID());
intent.putExtra("MEDICINA", item.getMedicina());
PendingIntent pendingIntent = PendingIntent.getActivity(this, item.getID(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, firstTime, intervalTime, pendingIntent);
AlarmBroadcastReceiver.java
public void onReceive(Context context, Intent intent) {
try {
int notifID = intent.getExtras().getInt("ID");
String medicina = intent.getExtras().getString("MEDICINA");
Intent alarmDetailsIntent = new Intent(context, AlarmDetails.class);
alarmDetailsIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("ID", notifID);
intent.putExtra("MEDICINA", medicina);
PendingIntent piDetails = PendingIntent.getActivity(context, notifID, alarmDetailsIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mbBuilder = new NotificationCompat.Builder(context);
mbBuilder.setSmallIcon(R.drawable.ic_menu_my_calendar);
mbBuilder.setContentTitle("Medic Reminder");
mbBuilder.setContentText(medicina);
mbBuilder.setVibrate(new long[]{100, 250, 100, 500});
mbBuilder.setContentIntent(piDetails);
notificationManager.notify(notifID, mbBuilder.build());
}catch (Exception ex){
ex.printStackTrace();
}
}
和當所述通知的用戶點擊,將必須打開活動並顯示帶ID額外警報的詳細信息,但getIntent.getExtras()始終爲NULL。
AlarmDetails.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recordatorio_alarma);
Bundle extras = getIntent().getExtras();
if (extras != null){
try {
int notifID = extras.getInt("ID");
String medicina = extras.getString("MEDICINA");
TextView txtMedicina = (TextView) findViewById(R.id.txtMedicinaAlarma);
txtMedicina.setText("Medicina de prueba");
Button btnCerrar = (Button) findViewById(R.id.btnStopAlarm);
btnCerrar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(notifID);
} catch (Exception e) {
e.printStackTrace();
}
}
}
,但這樣做是不一樣的? ''Bundle extras = getIntent()。getExtras();' 'int notifID = extras.getInt(「ID」);' 'String medicina = extras.getString(「MEDICINA」);' – gustav12
關鍵是MEDICINA不在你的臨時演員中。事實上,你還沒有設置額外的,這就是getExtras()返回null的原因。這就是我所關注的。對困惑感到抱歉。 – 2013-07-14 23:37:26
當然是: Intent alarmDetailsIntent = new Intent(context,AlarmDetails.class); alarmDetailsIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); intent.putExtra(「ID」,notifID); intent.putExtra(「MEDICINA」,medicina); – gustav12