繁体   English   中英

Android 通知活动不会启动

[英]Android Notification Activity Won't Launch

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

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

它启动的活动 PrivacyMessage 有一个 oncreate ,它将自己的 XML 文件设置为内容视图,并且应该显示出来。 但是,当单击通知时,它什么也不做,只是将您返回到您所在的活动。我的想法是我可能需要将新活动添加到清单中,但无法确定我应该添加的确切内容,以及在哪里。 这是我目前的清单:

<?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>

我希望我只是在某个地方错过了明显的东西。 但是有什么想法在哪里?

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

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

您应该在清单中创建第二个意图过滤器,专门用于通知处理。 然后在您的 onCreate / onResume 中,检查意图以了解您在哪种情况下并相应地填充您的视图。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM