开发者

Android Notification Activity Won't Launch

I have an android activity that also displays a notification on the notification bar, and a further description of it in the pulldown menu. The problem happens when trying to click the notification. From what I have seen in most other problems, putting a flag on it to clear other views should fix the problem. However this is the code I have:

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

The activity that it launches, PrivacyMessage has an oncreate that sets its own XML file to be the contentview, and should be showing up. However, when the notification is clicked, it does nothing, just returns you to the activity you were in. My thinking was that I might need to add the new activity into the manifest, but was having trouble figuring out exactly what I should be adding, and where. This is the current manifest that I have:

<?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:na开发者_JAVA百科me="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

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

I'm hoping that I'm just missing the blindingly obvious somewhere. But any ideas where?


First of all the flags don't force the creation of a new activity if at least one is available, so in most cases only the onResume method will be called (check the Activity lifecycle).

Then, if you have a single activity, how does it know what to display ? What you need to do is have a way to differentiate the intents between calling the app from a launcher, and calling it from a notification.

You should create a second intent filter in your manifest specifically for the notification handling. Then in your onCreate / onResume, check the intent to know in which case you are and populate your view accordingly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜