Не удается отправить данные через PendingIntent

#android #android-pendingintent

#Android #android-PendingIntent

Вопрос:

Я пытался установить уведомление с помощью PendingIntent.Но я не могу получить данные, которые я отправил.

это коды

Класс для отображения уведомлений (работает должным образом, я могу получить все данные из intent)

     protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

     //---get the notification ID for the notification; 
    // passed in by the MainActivity---
    int notifID = getIntent().getExtras().getInt("id");
    String date = getIntent().getExtras().getString("date");
    String time = getIntent().getExtras().getString("time");
    String text = getIntent().getExtras().getString("text");


    //---PendingIntent to launch activity if the user selects 
    // the notification---


    NotificationManager nm = (NotificationManager)
        getSystemService(NOTIFICATION_SERVICE);
    Notification notif = new Notification(
        R.drawable.ic_launcher, 
        text  " " date " @ " time,
        System.currentTimeMillis());
    notif.flags = Notification.FLAG_AUTO_CANCEL;

    CharSequence from = "TODO Note";
    CharSequence message = text;   


    Intent p = new Intent(this,NotifThrow.class);
    p.putExtra("id",notifID);  
    p.putExtra("date",date);

    PendingIntent detailsIntent = 
        PendingIntent.getActivity(this, 0, p, 0);


    notif.setLatestEventInfo(this, from, message, detailsIntent);

    //---100ms delay, vibrate for 250ms, pause for 100 ms and
    // then vibrate for 500ms---
    notif.vibrate = new long[] { 100, 250, 100, 500};        
    nm.notify(notifID, notif);

    //---destroy the activity---
    finish();
}   

}
 

этот класс (ниже) должен работать, когда я нажимаю на уведомление (он работает, но нет данных из intent)

     public class NotifThrow extends Activity 
    {
TodoDataSource dataSource;
SQLiteDatabase database;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    int notifID = getIntent().getExtras().getInt("id");
    String dates = getIntent().getExtras().getString("date");

    Log.i("Message From Notification Touch","Id" notifID);
    dataSource = new TodoDataSource(this);
    dataSource.alarmToggle(notifID);
    Intent intent = new Intent(this,MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    startActivity(intent);

}
 

идентификатор, дата отображаются как null

что я здесь делаю не так? Заранее спасибо..

Ответ №1:

Был ли обходной путь

установите общедоступный статический идентификатор int внутри класса NotifThrow.java и обновил его извне

Ответ №2:

Если вызываемый вами экземпляр activity уже запущен, то вместо создания нового экземпляра он вызовет onNewIntent() . getIntent() from onCreate всегда возвращает намерение, которое использовалось для запуска Activity (первое).