【问题标题】:Android notification Reorder to front doesn't work. Copied from APP that it has worked inAndroid 通知重新排序到前面不起作用。从它工作的APP复制
【发布时间】:2014-12-11 18:08:44
【问题描述】:

我从虚拟项目测试服务和通知中复制了一段代码,其中重新编码器到字体的工作就像一个魅力。

这是通知的代码(与教程中的非常相似)

    NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.ic_launcher)
    .setAutoCancel(true)
    .setContentTitle("Request awaiting")
    .setContentText("There is a service request awaiting for Your reaction");

    Intent resultIntent = new Intent(this, MainActivity.class);
    resultIntent.setAction(Intent.ACTION_MAIN);
    resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);//stackBuilder.getPendingIntent(0,  Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    nBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = nBuilder.build();
    mNotificationManager.notify(1, notification);

清单看起来像:

<application android:allowBackup="true" android:icon="@drawable/ld"
    android:label="@string/app_name" android:theme="@style/AppTheme">
    <activity 
        android:name="com.iraasta.cloudcab.driver.MainActivity"
        android:launchMode="singleTop"
        android:label="@string/app_name" 
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

它在我的其他应用程序中看起来完全一样,它完美地将应用程序带到前面,就像从主屏幕选择时一样。 但在这一次中,它每次都调用 OnCreate 并失去整个状态。

【问题讨论】:

  • 你的应用有透明/半透明的主题吗?
  • 如果您要问的话,它会在具有 Android 按钮栏样式的按钮上使用透明样式
  • 如果活动具有透明背景,有一个已知错误会破坏 FLAG_ACTIVITY_REORDER_TO_FRONT 的功能。也许那不是你的问题。由于您尝试启动的活动是您的根 (LAUNCHER) 活动,您为什么要在这里设置后台堆栈?没有家长活动。也许这是导致问题的原因?
  • 我只是复制了官方android通知教程中使用的所有代码
  • 我的应用是单一活动

标签: android android-intent notifications


【解决方案1】:

要将您的应用程序以任何状态置于最前面(如果它在后台),或启动您的应用程序(如果它尚未运行),您需要启动您的“根活动”( ACTION=MAIN 和 CATEGORY=LAUNCHER) 并设置标志Intent.FLAG_ACTIVITY_NEW_TASK。这将在新任务中启动 Activity(如果您的应用尚未运行),或将现有任务置于前台(如果您的应用已在运行)。

由于您是通过通知执行此操作,因此您应该在传递给通知的Intent 中设置Intent.FLAG_ACTIVITY_NEW_TASK。但是,如果您忘记设置此标志,Android 已经足够好。

我不确定为什么设置 Intent.FLAG_ACTIVITY_REORDER_TO_FRONT 会导致您的问题,但我可以想象这会以某种方式混淆 Android。

【讨论】:

    【解决方案2】:

    没有使用任何标志来修复它。

    致日后查看此帖子的人。
    如果您的应用使用单个活动并且您希望在单击通知后将其置于前面,只需将标志保留为整数 0。
    它解决了我的问题。
    感谢您的建议

    赏金给能解释为什么这面旗帜会毁掉一切的人。

    【讨论】:

      【解决方案3】:

      在清单中设置 android:launchMode="singleTask" 为你想带到前面的活动对我有用

      【讨论】:

        猜你喜欢
        • 2019-02-08
        • 2021-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多