gpt4 book ai didi

通知点击的Android调用方法

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

此代码创建一个通知。如果点击它,则运行当前应用程序( Intent 在Entry中创建,这是我唯一的Activity),一个Android开发者博客的稍微修改版本:

private void makeIntent() {
NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.prev, "Status message!", System.currentTimeMillis());
Intent intent = new Intent(this, Entry.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
note.setLatestEventInfo(this, "New Email", "Unread Conversation", pi);
note.flags |= Notification.FLAG_AUTO_CANCEL;
mgr.notify(NOTIFY_ME_ID, note);
}

但我不想启动任何 Activity ,而只想在当前 Activity 中运行一个方法。从我目前所读到的,我想我必须使用像 startActivityForResult() 这样的方法,使用 intent-filters 并实现 onActivityResult(),但是在搞砸了所有这些事情之后,改变了 IntentPendingIntent 中的东西,我仍然没有可用的结果。是否可以以某种方式调用 Entry 中的方法(我的主要 Activity,其中创建了 Intent),或者捕获任何传出或当我点击我新制作的Notification时传入Intents

PS。如果这是一个重复的线程,我很抱歉,所以现在很慢,我无法正确搜索。

最佳答案

在 list 文件的 activity 中添加 android:launchMode="singleTop",使用方法 protected void onNewIntent(Intent intent) { ... } 并使用以下代码:

private static final int MY_NOTIFICATION_ID = 1;
private NotificationManager notificationManager;
private Notification myNotification;

void notification() {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
myNotification = new Notification(R.drawable.next, "Notification!", System.currentTimeMillis());
Context context = getApplicationContext();
String notificationTitle = "Exercise of Notification!";
String notificationText = "http://android-er.blogspot.com/";
Intent myIntent = new Intent(this, YourActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(YourActivity.this, 0, myIntent, Intent.FILL_IN_ACTION);
myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
myNotification.setLatestEventInfo(context, notificationTitle, notificationText, pendingIntent);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
}

关于通知点击的Android调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13822509/

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