gpt4 book ai didi

android-intent - 带有附加功能的 Android 通知意图始终为空

转载 作者:行者123 更新时间:2023-12-03 17:49:19 24 4
gpt4 key购买 nike

我正在开发一个可以创建多个警报的应用程序,这些警报会向用户发送通知,因此他们可以点击通知并导致带有我想根据附加值显示的警报详细信息的事件,但我的 Actor 总是空的,我不明白这是怎么回事..我已经搜索了很多问题,似乎没有什么能解决我的问题。

这是创建带有唯一 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();
}
}

}

最佳答案

我发现了我的错误!

    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);

关于android-intent - 带有附加功能的 Android 通知意图始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17644647/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com