java - MIUI 10 不允许服务启动 Activity - 小米红米 Note

标签 java android miui

我的应用程序有一项服务和一项 Activity 。从服务中,使用以下代码调用 Activity :

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

即使没有标志, Activity 窗口通常也会以其正确的布局显示。 但是,在搭载 Android 7 的小米红米 Note 4 上,不显示 Activity 布局。我只在 logcat 上看到以下行:

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

我相信这不是 Android 7 (API 24) 问题,因为在另一台装有 Android 7 的设备上,服务可以成功启动 Activity。 我猜想,MIUI 正在阻止从服务启动 Activity。

我尝试更改 list 中定义 Activity 的方式。我还尝试了几个不同的标志。 我所有的测试都失败了。我无法成功启动该 Activity 。最糟糕的问题是日志中没有错误/异常。

请问对此有什么想法吗?

最佳答案

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

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

编辑:这是我使用的一段代码。我将其添加到 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);
}

更新:要检查是否授予此权限,您可以使用已运行的服务并启动虚拟透明 Activity ,然后在 onCreate 回调 LocalBroadcastManager 或类似,你就会知道这是理所当然的。这是一个丑陋的解决方案,但对某些人来说可能有用。

关于java - MIUI 10 不允许服务启动 Activity - 小米红米 Note,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59239077/

相关文章:

java - 构建一个在 Linux 服务器上运行的 jar

java - 在 Spring 中向 @ExceptionHandler 添加自定义消息

android - 如何使用 libvlc 在 surfaceView 上显示流式视频的进度条?

Android ImageButton 背景色

android - 后台定位服务在 oppo 和 MI 手机中不起作用

java - 如何修复此错误 :"Attempted to serialize java.lang.Class: org.springframework.beans.factory.annotation.Qualifier"

Java hashmap int 不能被取消引用

android - 导航组件中 Actions 和 Destination 的区别

android - 如何获取中国市场的广告ID(例如在MIUI和AliOS上)