gpt4 book ai didi

android - 如何设置通知的点击监听?

转载 作者:IT老高 更新时间:2023-10-28 23:08:51 27 4
gpt4 key购买 nike

我正在使用以下代码在通过 AlarmManager 启动服务时启动通知:

nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "App";
CharSequence message = "Getting Latest Info...";
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
Notification notif = new Notification(R.drawable.icon,
"Getting Latest Info...", System.currentTimeMillis());
notif.setLatestEventInfo(this, from, message, contentIntent);
nm.notify(1, notif);

我如何为这个项目设置一个 Intent ,以便当用户点击它时,它会启动某个 Activity ?

最佳答案

至于yoshi24的评论,你或许可以这样设置。

final Intent intent = new Intent(this, MyActivity.class);
intent.setData(data);
intent.putExtra("key", "value");
final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);

在处理未决 Intent 之前,您也需要了解这一点

https://stackoverflow.com/questions/1198558/how-to-send-parameters-from-a-notification-click-to-an-activity

更新像这样的东西对你有用

进入你的主要节日

<activity android:name=".MyActivity" android:launchMode="singleTop" ... />

在你的 Activity 中

@Override
protected void onCreate(Bundle savedInstanceState) {
processIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
processIntent(intent);
};

private void processIntent(Intent intent){
//get your extras
}

关于android - 如何设置通知的点击监听?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7184963/

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