【问题标题】:Android Notification use PendingIntent not new oneAndroid Notification 使用 PendingIntent 不是新的
【发布时间】:2014-06-22 10:05:53
【问题描述】:

我在我的应用程序中实现了一些通知。似乎每次我点击通知时都会启动一个新的意图。目标是将应用程序置于前台,因为它仍处于后台(因此不会被杀死)。

我怎样才能做到这一点?

mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, ChatActivity.class), 0);
    long[] vibration = new long[]{100, 250}; 

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.icon)
    .setAutoCancel(true)
    .setTicker(msg)
    .setLights(Color.WHITE, 300, 600)
    .setSound(Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.new_msg))
    .setVibrate(vibration)
    .setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

【问题讨论】:

    标签: java android android-intent notifications


    【解决方案1】:

    要将应用程序置于最前面,请在 Pending Intent 中使用以下 Intent。

    Intent intent = new Intent(this, ChatActivity.class);
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    

    如果尚未终止,这将恢复应用程序,如果已终止,则打开 ChatActivity。

    【讨论】:

    • 嗯,我在 ChatActivity 中有一个字符串。这个字符串有一些来自聊天的文本。单击通知后,活动出现但字符串为空。我不知道为什么:o 为了检查这个我把 System.out.println(bla);在 onResume() 中,当我点击通知时通常应该调用什么,对吧?
    • 为什么不把日志消息放在 onCreate 和 onResume 中,看看它们是否执行?但我认为 onResume 肯定会被调用。
    • 好的,我认为它现在可以工作了:o 我不知道错误是什么,但现在有了你的代码,我终于得到了通知之前的字符串,这就是我想要的。谢谢你:)
    • :) 但是,当按下返回按钮和按下主页按钮并且应用程序进入后台时,活动的行为会有所不同,并且您有时可能会丢失变量值。所以,请保持检查!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    • 2011-10-02
    • 2014-08-10
    • 1970-01-01
    相关资源
    最近更新 更多