Android 应用程序在关闭后仍在启动

Android 应用程序在关闭后仍在启动,android,Android,这是我的第一个活动,当我按下手机上的“后退”按钮时,应用程序关闭,但第二个活动在关闭后仍会弹出!可能是因为线程仍在运行?但我试着摧毁它,但没有用!有什么建议吗 protected void onCreate(Bundle myclass) { super.onCreate(myclass); setContentView(R.layout.splash); timer = new Thread() { public void run() {

这是我的第一个活动,当我按下手机上的“后退”按钮时,应用程序关闭,但第二个活动在关闭后仍会弹出!可能是因为线程仍在运行?但我试着摧毁它,但没有用!有什么建议吗

protected void onCreate(Bundle myclass) {
    super.onCreate(myclass);
    setContentView(R.layout.splash);
    timer = new Thread() {
        public void run() {
            try {
                sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } 
            finally {
                Intent openStarting = new Intent("nik.tri.MENU");
                startActivity(openStarting);
            }
        }
    };
    timer.start();
}

@Override
protected void onPause() {
    super.onPause();
    timer.destroy(); // tried timer.stop() as well 
    finish();
}

}尝试在OnDestroy方法中终止应用程序

int pid=android.os.Process.myPid();
 android.os.Process.killProcess(pid);

试试这个,它可能会帮助您尝试在OnDestroy方法中终止应用程序

int pid=android.os.Process.myPid();
 android.os.Process.killProcess(pid);

尝试此操作,可能会对您有所帮助。

是的,
线程
参与了该过程:

当你按下后退按钮时,你既不会破坏也不会停止你的应用程序,你只是让它不可见<此时会调用code>onPause(),但
finish()
只会终止
活动的工作流:该
活动仍在内存中,将在稍后的某个时间销毁。因此,
线程
保持运行。调用
Thread.stop()
效率低下:

此方法已弃用。因为以这种方式停止线程是不安全的,并且会使应用程序和VM处于不可预测的状态

因此
线程仍在运行,这就是应用程序在3秒钟后重新启动的原因。或者我应该说,开始一个新的
活动

我不明白你为什么要使用这个
线程
,它会在3秒钟后启动一个新的
活动
,但你应该:

  • 线程中使用
    布尔值来确定是否可以启动:

    timer = new Thread() {
        public void run() {
            try {
                sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } 
            finally {
                if (!paused) {
                    Intent openStarting = new Intent("nik.tri.MENU");
                    startActivity(openStarting);
                }
            }
        }
    };
    
  • 并在
    onPause()
    /
    onResume()
    中处理该标志:


是的,
线程
参与该过程:

当你按下后退按钮时,你既不会破坏也不会停止你的应用程序,你只是让它不可见<此时会调用code>onPause()
,但
finish()
只会终止
活动的工作流:该
活动仍在内存中,将在稍后的某个时间销毁。因此,
线程
保持运行。调用
Thread.stop()
效率低下:

此方法已弃用。因为以这种方式停止线程是不安全的,并且会使应用程序和VM处于不可预测的状态

因此
线程仍在运行,这就是应用程序在3秒钟后重新启动的原因。或者我应该说,开始一个新的
活动

我不明白你为什么要使用这个
线程
,它会在3秒钟后启动一个新的
活动
,但你应该:

  • 线程中使用
    布尔值来确定是否可以启动:

    timer = new Thread() {
        public void run() {
            try {
                sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } 
            finally {
                if (!paused) {
                    Intent openStarting = new Intent("nik.tri.MENU");
                    startActivity(openStarting);
                }
            }
        }
    };
    
  • 并在
    onPause()
    /
    onResume()
    中处理该标志: