Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么可以';我不能从服务转到主屏幕吗_Java_Android_Android Intent_Handler_Homescreen - Fatal编程技术网

Java 为什么可以';我不能从服务转到主屏幕吗

Java 为什么可以';我不能从服务转到主屏幕吗,java,android,android-intent,handler,homescreen,Java,Android,Android Intent,Handler,Homescreen,在API KitKat(19)Pixel 2上运行项目,目标API为30 我有一个在前台运行的服务,其中有一个处理程序,我可以通过我看到的println语句确认它是否正常运行 Handler handler = new Handler(Looper.getMainLooper()); private Runnable periodicUpdate = new Runnable() { @Override public void run() {

在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);
}
在我的服务之外,我的showHomeScreen()正在调用App类和getContext()方法:

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()方法的内容,它是关于如何将用户带到主屏幕的多个问题的首要答案,但是它一次也没有将我带到主屏幕。我错过了什么?如何将用户从我的服务带到主屏幕

谢谢大家!

免责声明:除非您向用户提供某种管理或家长控制类型的服务,否则不应使用此权限。 但是通过将此权限放入清单,您可以通过后台服务启动活动:

并通过将系统覆盖重新路由到设置来引导用户启用系统覆盖,以从后台服务启用应用程序的覆盖活动:
startActivity(新意图(设置、操作、管理、覆盖、权限))

在现代版本的Android中,您无法从后台启动活动。我可以从backgoround服务启动应用程序中的活动,但不能从主屏幕启动。更不用说,有许多应用程序可以将用户从给定的前台应用程序带到主屏幕。以应用程序阻塞应用程序为例。如果你说的是真的,这是不可能的@CommonsWareSee,谢谢。我注意到另一篇文章,你试图解决这样一个问题。我将删除/关闭此问题,并尝试一些替代方法,例如使用将使用showForeground()方法的receiver类@CommonsWare@UserOne我也希望得到一个错误,但它从来没有显示。这有点奇怪,因为我不认为我会在任何地方压制它。