无法在android 10 [android Q]中启动活动背景 [英] cannot start activity background in android 10 [ android Q ]

查看:115
本文介绍了无法在android 10 [android Q]中启动活动背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是android 10 [android Q,galaxy 10],

我使用的是android studio 3.3,

使用AVD,并制作了一个api 29 [android 10]虚拟电话.

在虚拟机上, 我执行我的应用程序,然后启动其他应用程序,例如日历,计算器. 这样我的应用程序活动就会进入后台模式.

当我在BroadcastReceiver上收到消息时. 我叫startActivity.

在这里,代码-> 公共类myReceiver扩展了BroadcastReceiver {}

public void onReceive(Context context, Intent intent)
{
        Intent intentRun = new Intent(context, LoginSuccess.class);
        intentRun.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intentRun);
}

,但没有显示LoginSuccess活动. [当我的应用程序处于后台模式时]

使用相同的代码,LoginSuccess活动显示得非常好 当我的应用程序处于前台模式时.

调用堆栈捕获图像

上图显示了调用堆栈 在我在广播接收器中调用startActivity之前.

我已阅读有关android 10后台活动问题的指南. [developer.android.com/~~一些位置]

在指导线

我知道 如果活动存在于调用堆栈中,则可以启动该活动 即使在后台模式下.

以上代码, 它会尝试启动最近调用堆栈中存在的活动.

为什么startActivity调用在后台模式下失败? [也许不会失败,但无论如何都不会激活到前台]

解决方案

在Android Q中,如果您的应用未包含下面链接中列出的那些例外,则无法从后台自动启动活动.

https://developer.android.com/guide/components/activities/background-starts

可能的解决方案:

1-您可以选择仅显示服务通知,然后单击一下即可开始待定意向

2-您可以使用全屏意图来显示您的意图,如其他答案所示,并由Google建议.

对于全屏意图解决方案,如官方文档中所述,系统UI可以选择显示提示通知,而不是在用户使用设备时启动该意图."

3-要在后台自动启动活动,我认为最可行的解决方案是添加"SYSTEM_ALERT_WINDOW"到清单文件.并在应用程序首次打开时请求一次用户许可. (用户可以手动授予此权限-(设置-应用-您的应用-高级-绘制其他应用")

请求许可的示例代码:

在清单中

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

应用中的某处:

 public static int ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE= 2323;

 private void RequestPermission() {
            // Check if Android M or higher
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                // Show alert dialog to the user saying a separate permission is needed
                // Launch the settings activity if the user prefers
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                        Uri.parse("package:" + getActivity().getPackageName()));
                startActivityForResult(intent, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);
            }
        }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (!Settings.canDrawOverlays(getContext())) {
                    PermissionDenied();
                }
                else
                {
                 // Permission Granted-System will work
            }

        }
    }
 }

I use android 10 [android Q, galaxy 10],

I use android studio 3.3,

using AVD, and made a api 29 [android 10] virtual phone.

at the virtual machine, I execute my app , after that, I launch other app like calendar, calculator. so my app activity get into background mode.

when I receive a message at BroadcastReceiver. I call startActivity.

here, code --> public class myReceiver extends BroadcastReceiver {}

public void onReceive(Context context, Intent intent)
{
        Intent intentRun = new Intent(context, LoginSuccess.class);
        intentRun.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intentRun);
}

but LoginSuccess activity do not shows up. [when my app is in background mode]

using same code, LoginSuccess activity show up very well when my app is in foreground mode.

call stack capture image

above image shows call stack right before I call startActivity in broadcast receiver.

I have read guide line for android 10 background activity issue. [developer.android.com/~~ some location]

at the guide line,

I came to know that if the activity exists in call stack, it can be started even in background mode.

above code, it try to start activity that exists in recent call stack.

why startActivity call fail in background mode ? [maybe not fail , but anyway not activated into foreground]

解决方案

With Android Q, it is impossible to start an activity from the background automatically if your app does not include those exceptions listed in the link below.

https://developer.android.com/guide/components/activities/background-starts

Possible Solutions:

1- You can choose just show a service notification, and start pending intent with a click

2- You can use full-screen intents to show your intent immediately as shown in the other answer and suggested by Google.

For full-screen intent solution, as described in the official document "The system UI may choose to display a heads-up notification, instead of launching this intent, while the user is using the device."

3- To start the activity automatically in the background, The most possible solution in my view is adding "SYSTEM_ALERT_WINDOW" to the manifest file. And ask for user permission once when the app opened the first time. (The user can give this permission manually - (Settings-Apps-Your App-Advanced- Draw over other apps))

Example code to request permission :

In Manifest:

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

Somewhere in app:

 public static int ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE= 2323;

 private void RequestPermission() {
            // Check if Android M or higher
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                // Show alert dialog to the user saying a separate permission is needed
                // Launch the settings activity if the user prefers
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                        Uri.parse("package:" + getActivity().getPackageName()));
                startActivityForResult(intent, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);
            }
        }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (!Settings.canDrawOverlays(getContext())) {
                    PermissionDenied();
                }
                else
                {
                 // Permission Granted-System will work
            }

        }
    }
 }

这篇关于无法在android 10 [android Q]中启动活动背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆