通知状态栏的活动意图?

发布于 2024-09-24 20:56:31 字数 1113 浏览 0 评论 0原文

我正在尝试从通知栏启动一个活动,但它需要获取一个整数。从一个活动到另一个活动,我只需将一个 int 放入意图中,但我似乎无法弄清楚在哪里为通知栏添加额外的内容?这可能吗?如果没有,有更好的方法吗?

public void notifyMe() {
    int icon = android.R.drawable.presence_away;
    CharSequence tickerText = "New Task Availible";
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);

    Context context = getApplicationContext();
    CharSequence contentTitle = "PRISM Task";
    CharSequence contentText = "New Task Availible";
    Intent notificationIntent = new Intent(this, TextAnswerActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notificationIntent.putExtra("taskId", 20);  //unable to retrieve 20 later

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

    final int HELLO_ID = 1;

    mNotificationManager.notify(HELLO_ID, notification);
}

我如何检索:

Intent myIntent = getIntent();
System.out.println("#######TaskID from Intent ===== " + myIntent.getIntExtra("taskId", -1));

I am trying to start an activity from the notification bar but it needs to get an int. From activity to activity I would just put an int into the intent but I can't seem to figure out where to add the extra for the notification bar? Is this possible? If not is there a better way to do it?

public void notifyMe() {
    int icon = android.R.drawable.presence_away;
    CharSequence tickerText = "New Task Availible";
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);

    Context context = getApplicationContext();
    CharSequence contentTitle = "PRISM Task";
    CharSequence contentText = "New Task Availible";
    Intent notificationIntent = new Intent(this, TextAnswerActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notificationIntent.putExtra("taskId", 20);  //unable to retrieve 20 later

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

    final int HELLO_ID = 1;

    mNotificationManager.notify(HELLO_ID, notification);
}

How I am retrieving:

Intent myIntent = getIntent();
System.out.println("#######TaskID from Intent ===== " + myIntent.getIntExtra("taskId", -1));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素衣风尘叹 2024-10-01 20:56:31

在创建以 notificationIntent 作为参数的 PendingIntent 之前,您必须调用 notificationIntent.putExtras(...) 。试试这个代码:

        Intent notificationIntent = new Intent(this, TextAnswerActivity.class);
        notificationIntent.putExtra("taskId", 20);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        Notification notification = new Notification(icon, tickerText, when);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 

并接收数据:

    Bundle b = getIntent().getExtras();
    if (b != null) {
        int taskId = b.getInt("taskId");
        System.out.println("############# taskID: " + taskId);
    }

You have to call notificationIntent.putExtras(...) before creating PendingIntent that takes notificationIntent as a parameter. Try this code out:

        Intent notificationIntent = new Intent(this, TextAnswerActivity.class);
        notificationIntent.putExtra("taskId", 20);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        Notification notification = new Notification(icon, tickerText, when);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 

And to receive the data:

    Bundle b = getIntent().getExtras();
    if (b != null) {
        int taskId = b.getInt("taskId");
        System.out.println("############# taskID: " + taskId);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文