MIUI 10不允许服务启动活动-小米Redmi注意 [英] MIUI 10 doesn't let service start activity - Xiaomi Redmi Note

查看:567
本文介绍了MIUI 10不允许服务启动活动-小米Redmi注意的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有一项服务和一项活动.在服务中,使用以下代码调用活动:

My app has a service and an activity. From the service, activity is called with following code:

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

即使没有这些标志,通常也会以正确的布局显示活动窗口. 但是,在具有Android 7的Xiaomi Redmi Note 4上,不会显示活动布局.我只在logcat上看到以下行:

even without the flags, normally the activity window is displayed with its correct layout. However, on Xiaomi Redmi Note 4 with Android 7, activity layout is not displayed. I only see the following line on logcat:

I/时间轴:时间轴:Activity_launch_request时间:281438674 intent:Intent {flg = 0x30000000 cmp = com.test.app/.MainActivity}

I/Timeline: Timeline: Activity_launch_request time:281438674 intent:Intent { flg=0x30000000 cmp=com.test.app/.MainActivity }

我认为这不是Android 7(API 24)问题,因为在具有Android 7的另一台设备上,服务可以成功启动该活动. 我猜是MIUI阻止了服务启动该活动.

I believe this is not an Android 7 (API 24) issue because on another device with Android 7, service can successfully start the activity. I guess, MIUI is preventing the launch of the activity from service.

我尝试更改清单中定义活动的方式.我还尝试了几种不同的标志. 我所有的测试都失败了.我无法成功开始活动.最糟糕的问题是日志中没有错误/异常.

I tried changing how the activity was defined in the manifest. I also tried with several different flags. All of my tests failed. I could not succeed in starting the activity. Worst issue is that there is no error/exception in the logs.

请问对此有何想法?

推荐答案

在系统设置应用程序的应用程序信息"屏幕中>其他权限>在后台运行时显示弹出窗口.

In App Info screen from the system Settings app > Other permissions > Display pop-up windows while running in the background.

这似乎是在MIUI 11中引入的.

This seems introduced in MIUI 11.

这是我使用的一段代码.我将其添加到RecyclerView的权限列表中.

here is a piece of code that I use. I add that into a permission list in a RecyclerView.

Class<?> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class);
String miui = (String) get.invoke(c, "ro.miui.ui.version.name");
if (miui != null && miui.contains("11")) {
            PermissionData mPopup = new PermissionData();
            mPopup.text = "Other permissions > Display pop-up while in background";
            mPopup.onClickListener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    Uri uri = Uri.fromParts("package", getPackageName(), null);
                    intent.setData(uri);
                    startActivity(intent);
                }
            };

            mPermissionData.add(mPopup);
}

更新:要检查是否已授予此权限,您可以使用已经运行的服务并启动虚拟的透明活动,然后在onCreate中调用LocalBroadcastManager或类似名称,您将知道它已被授予.这是一个丑陋的解决方案,但可能对某些人有用.

UPDATE: To check if this permission is granted you could use an already running service and launch a dummy transparent activity, and in the onCreate call back on a LocalBroadcastManager or similar and you'll know it's granted. It's an ugly solution, but it may be useful for some.

这篇关于MIUI 10不允许服务启动活动-小米Redmi注意的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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