Cannot Send data Through pendingIntent

白昼怎懂夜的黑 提交于 2020-01-06 21:29:15

问题


I was trying to set a notification using pendingIntent.But I cant recieve the data which I sent.

these are the codes

Class for displaying notification(Working properly I can get every data from 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();
}   

}

this class (below) should work when I touch on notification(it works but no data from 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);

}

id ,date are showing null

what am I doing wrong here ? Thanks in advance..


回答1:


Did a workaround

set a public static int id inside the class NotifThrow.java and updated it from outside




回答2:


If the instance of activity you are calling is already running then instead of creating new instance, it will call onNewIntent(). getIntent() from onCreate always returns the Intent which was used for launching Activity (the first one).



来源:https://stackoverflow.com/questions/24390733/cannot-send-data-through-pendingintent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!