Android 从服务启动活动

Android 从服务启动活动,android,service,android-activity,Android,Service,Android Activity,我正在使用安卓系统中的一项服务,它在一段时间后启动一项活动。 这是我的代码: Intent intent = new Intent(this, PopupActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); 问题是,如果我的应用程序在后台运行(暂停),并且服务启动PopupActivity,那么在后台运行的应用程序也会启动。 我不希望出现这种行为,有没有办法禁用它,以便只显

我正在使用安卓系统中的一项服务,它在一段时间后启动一项活动。 这是我的代码:

Intent intent = new Intent(this, PopupActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
问题是,如果我的应用程序在后台运行(暂停),并且服务启动PopupActivity,那么在后台运行的应用程序也会启动。
我不希望出现这种行为,有没有办法禁用它,以便只显示我的PopupActivity?

使用变量定义应用程序是否“暂停”以避免启动活动

private boolean isPaused = false;
if(!isPaused){
 Intent intent = new Intent(this, PopupActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}else{
Log.i("Activity", "Activity cannot started, app is onBackground");
}
如果您的应用程序已暂停,则isPaused=true

  @Override
        protected void onPause(){
            isPaused = true;
            super.onPause();
        }
如果您的应用程序执行
onResume()
方法,则isPaused=false

@Override
    protected void onResume(){
        isPaused = false;
    super.onResume();
    }
评估isPaused是否为false,然后启动反活动

private boolean isPaused = false;
if(!isPaused){
 Intent intent = new Intent(this, PopupActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}else{
Log.i("Activity", "Activity cannot started, app is onBackground");
}

不,在安卓系统中,所有建筑组件(包括活动)都在应用程序上下文中,如果不启动应用程序上下文,就无法显示您的活动,这与安卓系统的本质背道而驰,就像试图在不启动引擎的情况下启动汽车:p(再好的例子也想不到了),这没有任何意义


问候

“我在使用安卓系统中的一项服务,它会在一段时间后启动一项活动。”-永远不要这样做。如果我安装了一个这样的应用程序,当我在玩游戏的时候,在我的脸上推一个<代码>活动>代码>时,我马上回复那个应用程序。使用
通知
并为用户提供打开或不打开弹出窗口的选项。@Squonk好的,我试过了,请参阅相关帖子: