从Android中的单击通知获取数据 [英] Getting data from clicked notification in android

查看:161
本文介绍了从Android中的单击通知获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我需要有关如何从使用广播接收器设置的待定意图中获取数据的帮助。我想发生的是单击通知时获取ID的数据,这对于我的活动是必需的。

Hey guys I need help on how to get the data from my pending intent which is set using a broadcast receiver. What I want to happen is to get the data of an id when the notification is clicked which will be needed for my activity.

这就是我的额外福利

public class AlertReceiver extends BroadcastReceiver {

    private int id;
    // Called when a broadcast is made targeting this class
    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle bundle = intent.getExtras();
        String title = bundle.getString("title");
        String time = bundle.getString("time");
        id = bundle.getInt("id");

        createNotification(context, title, time, "Pharoah Reminder");
    }

    public void createNotification(Context context, String msg, String msgText,  String msgAlert){
        Intent reminderActivity =  new Intent(context, ReminderPreviewActivity.class);
        reminderActivity.putExtra("id", id);

        PendingIntent notificationIntent = PendingIntent.getActivity(context, id,
                reminderActivity, PendingIntent.FLAG_UPDATE_CURRENT );

        NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(msg)
                .setTicker(msgAlert)
                .setContentText(msgText)
                .setContentIntent(notificationIntent);

        mBuilder.setContentIntent(notificationIntent);
        mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);

        NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(id, mBuilder.build());
    }
}

但是当我尝试打开时,它始终为null通知中的我的活动。

but then it is always null when I try to open my activity from the notification.

这是我的获取方式。

public class ReminderPreviewActivity extends AppCompatActivity {

    private Toolbar mToolBar;

    private TextView titleTextView;
    private TextView descTextView;
    private TextView timeTextView;
    private TextView dateTextView;

    private String title;
    private String desc;
    private String date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reminder_preview);

        mToolBar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(mToolBar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);



        titleTextView   = (TextView) findViewById(R.id.titleTextView);
        descTextView    = (TextView) findViewById(R.id.descTextView);
        timeTextView    = (TextView) findViewById(R.id.timeTextView);
        dateTextView    = (TextView) findViewById(R.id.dateTextView);

        Intent extras = getIntent();

        if(extras.getStringExtra("title") != null){
            setContentFromExtras(extras);
        }else{
            setContentFromDB(extras);
        }
    }

    private void setContentFromExtras(Intent extras){
        title   = extras.getStringExtra("title");
        desc    = extras.getStringExtra("desc");
        date    = extras.getStringExtra("date");

        String[] dateDB = date.split(" ");

        titleTextView.setText(title);
        descTextView.setText(desc);
        timeTextView.setText(formatTime(dateDB[1])+" "+dateDB[2]);
        dateTextView.setText(formatDate(dateDB[0]));
    }

    public void setContentFromDB(Intent extras){
        String id = extras.getStringExtra("id");

        int reminderID = Integer.parseInt(id);

        titleTextView.setText(reminderID);
    }

我需要ID从数据库中检索数据。关闭应用程序时也会发生同样的事情。

I need the id to retrieve data from database. Same thing happens when I close the app.

推荐答案

在您的 AlertReceiver 中,您已声明

private int id;

,而您在<< c>中使用此 int 值/ p>

and you use this int value in

reminderActivity.putExtra("id", id);

因此,您还必须将其作为 int 在您的 setContentFromDB()方法中:

So you also have to get it as an int in your setContentFromDB() method:

int reminderID = extras.getIntExtra("id", someInt);
titleTextView.setText("" + reminderID);

其中 someInt应为 int

where 'someInt' should be an int value which is normally never used or a default value if that makes sense in your case.

您从 getStringExtra( id)<中得到null / code>,因为如果未找到具有键 id的字符串,则为返回值。

而且,如果将 int 值与 TextView.setText()一起使用,它将被解释为字符串资源。我认为您的情况( id代表数据库)很糟糕。

And if you use an int value with TextView.setText(), it will be interpreted as a string resource. I think in your case ('id' is meant for database) that's bad.

这篇关于从Android中的单击通知获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆