2014-04-01 164 views
-2

我正在使用Android中的服务,它在设定的时间段后开始活动。 这是我的代码:从服务开始活动

Intent intent = new Intent(this, PopupActivity.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(intent); 

问题是,如果我的应用程序在后台运行(暂停),并启动服务的PopupActivity,在后台运行的应用程序也会启动。 我不想这种行为,有没有办法来禁用这个,以便只显示我的PopupActivity?

+1

**“我正在使用Android中的服务,它会在一段时间后启动一项活动。”** - 绝对不要这样做。如果我安装了这样的应用程序,并在玩游戏,回复短信或电子邮件等时推送了一个“Activity”,我会立即卸载该应用程序。使用'通知'并给用户选择打开你的弹出或不弹出。 – Squonk

+0

@Squonk好吧,我试过这样做,请参阅相关文章:http://stackoverflow.com/questions/22793443/click-on-notification-doesnt-start-activity – deimos1988

回答

0

不,在Android中所有建筑构件(包括活动)现场应用环境中,没有显示你的活动方式如果没有启动你的应用程序环境,它违背了Android的本质,就像试图在没有启动引擎的情况下启动一辆车:P(不能想象一个更好的例子),它没有任何意义......

关心!

0

使用一个变量来定义,如果你的应用程序是“暂停”,以避免在启动活动

private boolean isPaused = false; 

如果您的应用程序暂停,然后isPaused得到= TRUE;

@Override 
     protected void onPause(){ 
      isPaused = true; 
      super.onPause(); 
     } 

如果应用程序执行onResume()方法,然后isPaused得到= FALSE;

@Override 
    protected void onResume(){ 
     isPaused = false; 
    super.onResume(); 
    } 

评估是否isPaused得到isn't为假,则启动日活动

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"); 
}