当通知被点击时如何启动一个活动?

4

我在我的安卓应用中遇到了一个奇怪的问题。我创建了一个通知,并希望在通知被点击时启动一个新的活动。问题是,当我点击通知时什么也没有发生,我不知道问题出在哪里?有人能帮帮我吗?以下是我的代码:

notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence NotificationTicket = "Notification";
CharSequence NotificationTitle = "Notification";
CharSequence NotificationContent = "Test";
long when = System.currentTimeMillis();

Notification notification = new Notification(R.drawable.icon,
NotificationTicket, when);

Context context = getApplicationContext();

Intent notificationIntent = new Intent(this, ShopsOnMap.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);

notification.setLatestEventInfo(context, NotificationTitle, NotificationContent, contentIntent); 
notificationManager.notify(NOTIFICATION_ID, notification);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP); 

在LogCat中,我有以下内容:“05-14 11:40:35.858: INFO/ActivityManager(59): Starting activity: Intent { cmp=com.ShoppingList/.ShopsOnMap bnds=[0,101][320,165] }”。 - Gaby
好的,我假设你有一个名为“ShopsOnMap”的活动,因为你正在调用它。你说“没有什么发生”,但我建议有些事情确实发生了,但该类中的另一个问题使其无法启动。例如,意图中缺少捆绑包(“extra”)。因此,您应该打开ShopsOnMap.java文件,找到“onCreate”函数,并添加一个“Log.d()”调用。然后读取日志文件,看看它是否在运行。 - Nanne
啊,我们的评论交叉了。看起来意图已经被调用了。所以问题可能出在shopsonmap类中?你通常会如何启动该应用程序?使用该类吗? - Nanne
我没有收到任何错误信息,所以我不知道问题出在哪里。在类 ShopsOnMap 中,我只有以下代码:public class ShopsOnMap extends Activity{ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }} - Gaby
我希望当通知被点击时,类ShopsOnMap能够启动。我在其他类中没有使用这个类。你能提供一个可以工作并启动新活动的示例吗?这会对我很有帮助。 - Gaby
显示剩余4条评论
4个回答

9
我曾遇到过类似的问题,后来发现我没有在清单文件中声明我的新活动。
<activity
        android:name=".YourActivityHere">
        </activity>

3

您需要为Intent设置操作和类别。

如果此活动不是应用程序的入口点:

Intent notificationIntent = new Intent(context, ShopsOnMap.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent myIntent = PendingIntent.getActivity(context, 0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

如果if是您的应用程序的入口点,则可以通过xml获得它,如下所示:

<activity
        android:name=".ShopsOnMap"
        android:theme="@android:style/Theme.NoTitleBar"
        android:windowSoftInputMode="adjustResize"
          android:configChanges="orientation"
        android:screenOrientation="portrait"
         >
        <intent-filter>
            <category android:name="android.intent.category.LAUNCHER" />

            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
</activity>

所以,只需要添加它:

Intent notificationIntent = new Intent(context, ShopsOnMap.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent myIntent = PendingIntent.getActivity(context, 0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

它对我有效。


2

现在我知道这个问题非常老,但对我来说解决方案是改变requestCode。显然,当requestCode为0时,有时它不起作用。它在我的一个手机上工作,但在另一个手机上无法工作。


1
谢谢!那解决了我的问题。 - Trees

0

如评论所述:我无法看出您的代码有任何问题,怀疑您的shopsonmap只是没有显示任何内容。以下是我使用并且有效的代码。

private void setNotifiy(){
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

    int icon = R.drawable.notification_icon;//  R.drawable.notification_icon;
    CharSequence tickerText = "Tickertext goes here :) !";
    long when = System.currentTimeMillis();
    Context context = getApplicationContext();
    CharSequence contentTitle = "ThisIsYourTitle";
    CharSequence contentText = "some content goes here";

    Notification notification = new Notification(icon, tickerText, when);
    Intent notificationIntent = new Intent(this, MyClass.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);    
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    mNotificationManager.notify(NOT_ID, notification);
}

我发现问题出在我的类ShopsOnMap上。我尝试使用另一个类,它可以工作。但是我现在有一个新问题。如果我为此创建一个新类,我会得到错误NoClassDefFound...我已经搜索了很多解决方案,但没有找到任何帮助我的东西。你知道这个错误的解决方案吗? - Gaby
你在清单文件中添加了吗?也许可以将此标记为已解决,并提供一些代码(类、调用和清单文件)来发起另一个问题? - Nanne

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接