gpt4 book ai didi

android - 通知状态栏的 Activity Intent ?

转载 作者:搜寻专家 更新时间:2023-11-01 08:16:44 24 4
gpt4 key购买 nike

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

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));

最佳答案

您必须在创建以 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);
}

关于android - 通知状态栏的 Activity Intent ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3766396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com