【问题标题】:Always go to the class that given at intent总是去有意安排的课程
【发布时间】:2014-04-05 07:44:32
【问题描述】:

我已经测试了大约 4 或 5 天的通知意图。我对自己失望了,我无法达到我的目标。我认为这不是太难,但我无法得到它。单击通知时,我想进行当前的运行活动。我有意使用了很多东西。

   notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
   notificationIntent.setAction(Intent.ACTION_MAIN);
   notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);

在pendingIntent中,

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

在清单中,

    android:launchMode="singleTop" // android:launchMode="singleTask" // android:launchMode="singleInstance"

但它总是按照我的意图上的课。有人帮助我,我不知道。

【问题讨论】:

  • 也许这个 SO link 可能会帮助你。
  • @Halo 你当前的活动名称是什么,notificationIntent 持有哪个活动?
  • MainActivity 是当前活动,Goo.class 在 notificationIntent。
  • 好的,那么您不可能打开 mainActivity(因此您的意图应该指向 MainActivity 而不是 Goo 类)并覆盖 mainactivity 中的 onNewIntent() 方法并从中打开 Goo.class。
  • @Halo 我编辑了我的答案,看这里stackoverflow.com/questions/22834790/…

标签: android android-intent notifications android-pendingintent


【解决方案1】:

尝试类似:

@SuppressLint("NewApi")
public static void showNotification(Context context, String message) {
    Intent intent = new Intent(context, MainActivity.class);
    // Ensures navigating back to activity, then to Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    // Add the activity parent chain to the stack builder.
    stackBuilder.addParentStack(MainActivity.class);
    // Add a new Intent to the task stack.
    stackBuilder.addNextIntent(intent);     
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    // Create notification.
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        // API 16 onwards.
        Notification.Builder builder = new Notification.Builder(context)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setContentTitle(context.getString(R.string.app_name))
            .setSmallIcon(R.drawable.ic_notifier)
            .setTicker(message)
            .setContentText(message)
            .setPriority(Notification.PRIORITY_HIGH)
            .setStyle(new Notification.BigTextStyle().bigText(message)) 
            .setWhen(System.currentTimeMillis());
            mNotificationManager.notify(NOTIFICATION_DEFAULT, builder.build());         
    } else {
        // API 15 and earlier.
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setContentTitle(context.getString(R.string.app_name))
            .setSmallIcon(R.drawable.ic_notifier)
            .setTicker(message)
            .setContentText(message)
            .setPriority(Notification.PRIORITY_HIGH)
            .setWhen(System.currentTimeMillis());
        mNotificationManager.notify(NOTIFICATION_DEFAULT, builder.build());         
    }
}

【讨论】:

  • 您不必马上勾选我的答案。只有在它对您有用时才勾选它,以便将来阅读您帖子的其他人知道它有效并且他们也可以使用我的答案!
  • 请原谅,但在使用时可以吗。
  • 我倾向于在 BOOT_COMPLETED 事件中使用通知,或者当我通过 Intent 处理从 BroadcastReceiver 接收到的信息时。我还没有遇到需要在服务中使用通知的情况——但我想它会起作用。如果您的服务在内存不足的情况下被 Android 杀死,您可能需要进行更多测试。那么,在这种情况下,您的通知会发生什么变化。
  • 可能是在某个地方调用了stackBuilder?我不知道 stackBuilder 是干什么用的。
  • 更多关于TaskStackBuilder的信息可以在here找到。简而言之,它会保留新任务的回溯堆栈或历史记录。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-31
相关资源
最近更新 更多