gpt4 book ai didi

Android 通知 Activity 不会启动

转载 作者:行者123 更新时间:2023-11-29 14:06:36 26 4
gpt4 key购买 nike

我有一个 android Activity ,它还在通知栏上显示通知,并在下拉菜单中对其进行进一步描述。尝试单击通知时会出现问题。从我在大多数其他问题中看到的情况来看,在其上放置一个标志以清除其他 View 应该可以解决问题。然而,这是我的代码:

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

int icon = R.drawable.stat_sys_secure;
CharSequence tickerText = "Browser Security Enabled";
long when = System.currentTimeMillis();

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

Context context = getApplicationContext();
CharSequence contentTitle = "Browser Security";
CharSequence contentText = "Security Vulnerability Detected";
Intent notificationIntent = new Intent(this, PrivacyMessage.class);

//Should clear current view, and show this new activity
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);

它启动的 Activity PrivacyMessage 有一个 oncreate 将它自己的 XML 文件设置为内​​容 View ,并且应该显示出来。然而,当通知被点击时,它什么都不做,只是让你回到你所在的 Activity 。我的想法是我可能需要将新 Activity 添加到 list 中,但无法确定我应该添加什么,以及在哪里。这是我当前的 list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.browser"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="@drawable/icon"
android:label="@string/app_name"
android:debuggable="true">
<activity android:name=".Browser"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>

我希望我只是错过了某处非常明显的地方。但是有什么想法吗?

最佳答案

首先,如果至少有一个 Activity 可用,则标志不会强制创建新 Activity ,因此在大多数情况下只会调用 onResume 方法(检查 Activity lifecycle)。

那么,如果您只有一个 Activity,它如何知道要显示什么?您需要做的是有一种方法来区分从启动器调用应用程序和从通知调用应用程序之间的 Intent 。

您应该在 list 中专门为通知处理创建第二个 Intent 过滤器。然后在您的 onCreate/onResume 中,检查 Intent 以了解您在哪种情况下并相应地填充您的 View 。

关于Android 通知 Activity 不会启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6602730/

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