3

我正在开发一个可以创建多个警报的应用程序,这些警报会向用户发送通知,因此他们可以点击通知并导致带有警报详细信息的活动,我希望根据附加项的值显示该活动,但是我的额外内容总是空的,我不明白它出了什么问题..我已经搜索了很多问题,但似乎没有任何东西可以解决我的问题。

这是创建警报的地方,每个警报都有一个唯一的 ID。“项目.getID()”

新警报.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();
        }
    }

}
4

2 回答 2

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);
于 2013-07-15T12:11:42.740 回答
4

你想要getStringExtra(),而不是getExtras().getString()。这可以解决您还没有任何附加功能的情况。

于 2013-07-14T23:09:55.403 回答