【问题标题】:Android Status Bar Notifications - Opening the correct activity when selecting a notificationAndroid 状态栏通知 - 选择通知时打开正确的活动
【发布时间】:2010-05-26 21:45:56
【问题描述】:

我遇到了一个问题,通知在被点击时没有打开/转到正确的活动。

我的通知代码(位于扩展服务的类中):

Context context = getApplicationContext();

    CharSequence contentTitle = "Notification";

    CharSequence contentText = "New Notification";

    final Notification notifyDetails =
        new Notification(R.drawable.icon, "Consider yourself notified", System.currentTimeMillis());

    Intent notifyIntent = new Intent(context, MainActivity.class);

    PendingIntent intent =
          PendingIntent.getActivity(context, 0,
          notifyIntent,  PendingIntent.FLAG_UPDATE_CURRENT | Notification.FLAG_AUTO_CANCEL);

    notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);

    ((NotificationManager)getSystemService(NOTIFICATION_SERVICE)).notify(NOTIFICATION_ID, notifyDetails);

如果我在创建服务的应用程序打开时单击通知,通知会消失(由于 FLAG_AUTO_CANCEL)但活动不会切换。

如果我从主屏幕单击通知,通知会消失,我的应用程序会被带到最前面,但它仍保留在进入主屏幕之前打开的 Activity 上,而不是进入主屏幕。

我做错了什么?如何指定将被拉起的活动?

【问题讨论】:

    标签: android android-activity notifications


    【解决方案1】:

    实际上可能已经回答了我自己的问题:

    Intent notifyIntent = new Intent(Intent.ACTION_MAIN);
    notifyIntent.setClass(getApplicationContext(), Main.class);
    

    【讨论】:

    • 感谢您分享您的答案,请您提及“Main.class”包含的内容吗?
    • 什么是 getApplicationContext() ?
    • Main.class 将是您要打开/转到的活动。而getApplicationContext是Activity类中的一个方法,继承自ContextWrapper类。 developer.android.com/reference/android/content/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多