【问题标题】:Android: How to resume an App from a Notification?Android:如何从通知中恢复应用程序?
【发布时间】:2010-10-28 22:35:57
【问题描述】:

我正在尝试将通知编程为恢复我的应用程序,而不是简单地启动我的应用程序的新实例...我基本上是在寻找它来做与长按主页按钮和应用程序从那里恢复。

这是我目前正在做的事情:

void notifyme(String string){

    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager)
                                                getSystemService(ns);

    int icon = R.drawable.notification_icon;        // icon from resources
    CharSequence tickerText = string + " Program Running...";     // ticker-text
    long when = System.currentTimeMillis();         // notification time
    Context context = getApplicationContext();      // application Context
    CharSequence contentTitle = *********;  // expanded message title
    CharSequence contentText = string + " Program Running...";//expanded msg text

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

    // the next two lines initialize the Notification, using the configurations
    // above
    Notification notification = new Notification(icon, tickerText, when);
    notification.setLatestEventInfo(context, contentTitle, contentText,
                                                                contentIntent);
    final int HELLO_ID = 1;
    mNotificationManager.notify(HELLO_ID, notification);
}

我猜测新的 Intent 行是问题所在...任何帮助将不胜感激!

【问题讨论】:

标签: android


【解决方案1】:

你需要设置你的标志

 notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;   
 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

另外,如果您永远不希望有重复的活动,请在清单中给它这个属性

android:launchMode="singleTask"

【讨论】:

  • 这仅在应用程序通过主页按钮退出时有效......但在应用程序通过后退按钮退出时不起作用......知道如何解决这个问题???跨度>
  • 在您的 notificationIntent 中设置动作以指示您正在恢复,在 oncreate 中检查该动作并采取相应的行为,在意图中的捆绑中传递您需要正确恢复的任何数据
  • 注册。 @FrankBozzo 的评论:默认行为是关闭/销毁后退按钮上的活动(这至少是 adb logcat 告诉我的) - 所以你不能恢复到被杀死的活动......但是我相信你可以覆盖默认后退按钮行为(请参阅WebChromeClient 事件)不退出应用程序。
【解决方案2】:

选择的答案对我不起作用,但如果其他人查看此内容,这对我有用:Resume application and stack from notification

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多