为什么我不能从服务转到主屏幕

rn0zuynd  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(273)

在api kitkat(19)pixel 2上运行项目,目标api为30。
我有一个在前台运行的服务,其中有一个处理程序,我可以通过看到的println语句确认它是否正常运行。

Handler handler = new Handler(Looper.getMainLooper());
    private Runnable periodicUpdate = new Runnable() {
        @Override
        public void run() {
            handler.postDelayed(periodicUpdate, 1000 - SystemClock.elapsedRealtime()%500);
            currentApp = getForegroundApp();
            for (AppObject restrictedApp : allAppsRestricted) {
                System.out.println("restricted app: " + restrictedApp.packageName);
                System.out.println("foreground app: " + currentApp);
                if (currentApp.equals(restrictedApp.packageName)) {
                    showHomeScreen();
                    System.out.println("We're trying to go home");
                }
            }
        }
    };

我还可以通过我看到的println语句确认当前应用程序和前台应用程序包是相同的。在我的服务中,我还有一个showhomescreen()方法。showhomescreen()方法的代码通过向用户显示主屏幕,理想地将用户从与受限制应用程序匹配的前台应用程序中带出。在这里:

public void showHomeScreen(){        
        Intent startMain = new Intent(Intent.ACTION_MAIN);
        startMain.addCategory(Intent.CATEGORY_HOME);
        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        App.getContext().startActivity(startMain);
}

在我的服务之外,我有app类和getcontext()方法,我的showhomescreen()正在调用:

public class App extends Application {
    private static Context context;

    @Override
    public void onCreate() {
        super.onCreate();

        createNotificationChannel();
        App.context = getApplicationContext();
    }

    public static Context getContext() {
        return context;
    }
}

我已经看到了showhomescreen()方法的内容,它是关于如何将用户带到主屏幕的多个问题的首选答案,但是它没有一次将我带到主屏幕。我错过了什么?如何将用户从我的服务带到主屏幕?
谢谢您!

6tr1vspr

6tr1vspr1#

免责声明:除非您向用户提供某种管理或家长控制类型的服务,否则不应使用此权限。但是。。。通过将此权限放入清单中,您可以通过后台服务启动活动: <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> 通过引导用户启用系统覆盖,将其重新路由到设置以从后台服务启用应用程序的覆盖活动: startActivity(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION));

相关问题