【问题标题】:Change android activity while app is in background?在应用程序处于后台时更改 android 活动?
【发布时间】:2015-11-13 04:58:17
【问题描述】:

我的 android 应用程序使用 ComponentCallbacks2 检测应用程序是否在后台(比如用户按下主菜单)。现在,我希望应用在当前在后台更改活动而不更改用户界面。

我唯一的问题是:如何更改用户在后台时的活动,以便在他/她返回应用程序时弹出,而不是一打电话startActivity(Intent i)就弹出。

以下是我希望我的应用执行的步骤:

  1. 用户在应用中打开主菜单 (WORKS)
  2. 应用检测到应用在后台 (WORKS)
  3. 使用意图,打开另一个活动,而不会中断用户正在使用的任何应用程序(不工作)

目前,第一步和第二步进展顺利。但是,当我使用意图更改活动时,活动会弹出打开......打扰用户。基本上,我想加载 Activity,以便一旦用户打开应用程序备份,它就会进入游戏结束屏幕。


我对该主题的研究,以及我自己尝试解决的问题:

我已经对这个主题进行了很多研究,但其他问题是关于在后台运行进程。为此,显然,人们会使用一项服务。我的问题是激光专注于如何更改用户在后台时的活动,以便在他/她返回应用程序时弹出。我也尝试使用 onContinue() 方法,但是每次用户更改活动时都会调用该方法,从而使其不可靠。

感谢专家的建议:

丰富

【问题讨论】:

    标签: java android multithreading android-intent android-activity


    【解决方案1】:

    我的建议是将您的首选项保存在 onStop 中(或当应用程序处于后台模式时)。然后在 SplashActivity 的 OnCreate 中使用该信息来启动 Game-Over 活动。

    ##

    在您检测背景的活动中

     @Override
    protected void onStop(){
       super.onStop();
    
    
      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putBoolean("isGameOver", isGameOver);
    
      // Commit the edits!
      editor.commit();
    }
    
    ######### 飞溅活动
    public class SplashActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
    
        Thread timerThread = new Thread(){
            public void run(){
                try{
                    sleep(2000);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }finally{
    
       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
       boolean isGameOver = settings.getBoolean("isGameOver", false);
                   if(isGameOver) {
                    Intent intent = new Intent(SplashActivity.this,GameOverActivity.class);
                    startActivity(intent);
     } else {
            // Start Main Activity
        }
                }
               }
            }; timerThread.start();
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }
    }
    

    【讨论】:

    • 感谢您的回答,但是开始时的二十秒计时器有什么意义?一旦我得到这个,我会标记你的最佳答案并喜欢你的帖子,非常感谢!
    • 当我尝试编写代码(没有线程)时,我的应用程序就冻结了?
    • 我已将您标记为最佳答案并喜欢。不过,我不得不更改答案,因为您的方法冻结了我的应用程序。相反,我只是使用了一个布尔值。
    • HI.. 你可以减少计时器。 2000 --> 500。启动应用程序时,启动程序会显示启动图像。您可以在许多应用程序中观察到这种行为。但是,您可以更改时间。
    • 我还有一个问题。如何在显示初始屏幕时加载我的广告?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多