我正在开发一个可以创建多个警报的应用程序,这些警报会向用户发送通知,因此他们可以点击通知,这会导致我想要显示的警报详细信息的活动,具体取决于额外的,但我的额外的东西总是空的,我不明白它是什么错误..我已经搜索了很多问题,似乎没有什么能解决我的问题。
这是创建具有唯一ID的警报的地方。 “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();
}
}
}
答案 0 :(得分:6)
Intent alarmDetailsIntent = new Intent(context, AlarmDetails.class);
alarmDetailsIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("ID", notifID);
intent.putExtra("MEDICINA", medicina);
必须是:
alarmDetailsIntent.putExtra("ID", notifID);
alarmDetailsIntent.putExtra("MEDICINA", medicina);
答案 1 :(得分:4)
你想要getStringExtra(),而不是getExtras()。getString()。这样可以解决您还没有任何额外内容的情况。