20

Android N使用分屏时,我想activity在用户单击通知时在当前活动窗口中启动,但如果通过单击通知启动, Android N总是在第二个窗口中启动。activity

NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.notification)
                        .setAutoCancel(false)
                        .setContentTitle("Demo Title")
                        .setContentText("Demo");

Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("myIntent", "test");

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);

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

Notification notification = mBuilder.build();

notification.flags = Notification.FLAG_NO_CLEAR;

mNotificationManager.notify(156, notification);

当 getintent比 launch activity

 Intent in = new Intent(MainActivity.this, SecondActivity.class);
 startActivity(in);

前 - 我在前台有两个应用程序,例如第一个窗口中的 Chrome 浏览器第二个窗口中的 Facebook现在我正在 Chrome 浏览器中搜索某些内容,此时我收到通知Gmail。现在,当我单击Gmail 通知时,Gmail 应用程序会在第二个窗口中通过替换Facebook打开,但我希望我的应用程序通知替换第一个窗口中的Chrome(与用户交互)。

4

3 回答 3

5

你不能

在 an 中指定以Intent在您的应用程序不属于的任务中启动 Activity,其中包括其他应用程序的多窗口窗口。

有几种可能的情况:

  1. 你从另一个Activity启动一个Activity,它会在前一个Activity的任务(和窗口)中启动。
  2. 您可以Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT在意图中包含该标志,并使用该Intent.FLAG_ACTIVITY_NEW_TASK标志在另一个窗口中启动 Activity。这仅在系统已经处于多窗口模式且无法保证时才有效。如果系统处于自由格式模式(而不是分屏模式),这请求在另一个自由浮动窗口中启动 Activity,那么您还可以使用.setLaunchBounds()指定它的大小。
  3. 你从一个Service启动一个Activity,因为一个Service不能在一个task中,你必须指定Intent.FLAG_ACTIVITY_NEW_TASKflag,系统会为这个Activity启动一个新的task。
  4. 你从一个通知(PendingIntent)开始一个活动,就像你做的那样:
    • 您可以Intent转到已经开始的活动并在其中做任何您想做的事情。例如,在另一个窗口中使用Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT.
    • 如果您的活动没有任务/窗口,系统将选择从哪里开始,您无法控制它。您可以使用 控制在自由格式模式下从何处启动 Activity .setLaunchBounds(),但不能像您想要的那样劫持另一个应用程序的窗口。

简而言之:您可以创建新窗口,也可以更改现有窗口,您不能从其他应用程序中获取窗口。

但如果你坚持

在控制您的新活动将在分屏多窗口中占据屏幕的哪一侧,然后创建一个新的虚拟活动。而不是开始你SecondActivity的虚拟活动。在其中,检查它从屏幕的哪一侧开始。我现在能想到的一种方法是在 a上调​​用 .getGlobalVisibleRect()View并查看这些值与整个屏幕大小的比较。您可以通过以下方式获取屏幕尺寸:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

您还可以调整检测键盘大小的解决方案以检测窗口的位置。虽然它更加hacky。

现在,如果您在所需的屏幕一侧,请SecondActivity正常启动finish()虚拟屏幕。如果你用错了,也使用Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT标志,你的 Activity 将从你想要的一侧开始,而虚拟 Activity 消失。

这是一个肮脏的黑客。我建议让系统选择从哪里开始您的活动。

如果你总是想替换一个打开的浏览器

然后让浏览器打开您的应用程序,它将占用它的任务(和窗口)。您可以通过打开Intent浏览器将处理的带有 的链接并从那里重定向到打开您的应用程序的链接来做到这一点,有关如何执行此操作的更多信息,请参阅处理应用程序链接启用深层链接。它应该工作。

于 2016-09-22T14:59:37.783 回答
1

您不能将活动用于此功能...

解决方案是使用片段而不是活动......

只需随时更换片段

于 2016-09-28T04:11:04.297 回答
1

你能检查下面的代码:

private void Notify(String notificationMessage, String pushFullMessage) {

    Intent intent = new Intent(this, GCMNotificationActivity.class);
    intent.putExtra("pushNotification", pushFullMessage);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    final int not_nu = generateRandom();
    PendingIntent pendingIntent = PendingIntent.getActivity(this,
            not_nu, intent, PendingIntent.FLAG_ONE_SHOT);


    android.support.v4.app.NotificationCompat.Builder notificationBuilder = new android.support.v4.app.NotificationCompat.Builder(
            this)
            .setSmallIcon(R.drawable.app_logo) //Small Icon from drawable
            .setContentTitle(this.getString(R.string.app_name))
            .setContentText(notificationMessage)
            .setAutoCancel(true)
            .setLocalOnly(true)
            .setColor(this.getResources().getColor(R.color.green))
            .setStyle(
                    new android.support.v4.app.NotificationCompat.BigTextStyle().bigText(notificationMessage))
//       .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher)) //Big Icon from drawable
            .setContentIntent(pendingIntent);


    notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
    notificationBuilder.setDefaults(Notification.DEFAULT_SOUND);
    notificationBuilder.setVibrate(new long[]{0, 100, 200, 300});

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

    notificationManager.notify(not_nu, notificationBuilder.build());
}

public static int generateRandom() {
    Random random = new Random();
    return random.nextInt(9999 - 1000) + 1000;
}
于 2016-09-28T04:47:05.253 回答