我找到了一些以编程方式退出Android应用程序的代码。通过在onDestroy()中使用以下任何代码,它会完全退出应用程序吗?

System.runFinalizersOnExit(真正的) (或) android.os.Process.killProcess (android.os.Process.myPid ());

我不想在点击退出按钮后在后台运行我的应用程序。 请告知我是否可以使用这些代码中的任何一个来退出我的应用程序?如果可以,我可以使用哪个代码?在Android中退出应用程序是一个好方法吗?


当前回答

我发现从一个活动中退出应用程序的最简单的方法是,不破坏Android的逻辑,也不需要在现有的活动中添加更多的代码和传递额外的东西:

public static void quitApplication (Activity currentActivity) {
    Intent intent = new Intent (currentActivity, QuitApplicationActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    currentActivity.startActivity (intent);
    currentActivity.finish ();
}

QuitApplicationActivity是:

public class QuitApplicationActivity extends AppCompatActivity {

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);

        finish ();
    }
}

其他回答

android中没有退出应用程序, SampleActivity.this.finish ();将完成当前活动。

当你从一个活动切换到另一个活动时,要坚持完成前一个活动

Intent homeintent = new Intent(SampleActivity.this,SecondActivity.class);
startActivity(homeintent);
SampleActivity.this.finish();

正确和准确的解决方案退出应用程序的按钮点击是使用下面的代码:

//On Button Back按下事件

public void onBackPressed()
{ 
   moveTaskToBack(true);
   finish();
}

首先,这种方法要求最小Api 16。

为了更广泛地解决这个问题,我将把这个解决方案分为3个部分。

1. 如果你想在Activity中退出应用程序,请使用以下代码片段:

if(Build.VERSION.SDK_INT>=16 && Build.VERSION.SDK_INT<21){
    finishAffinity();
} else if(Build.VERSION.SDK_INT>=21){
    finishAndRemoveTask();
}

2. 如果你想退出一个非Activity类的应用程序,可以访问Activity,那么使用下面的代码片段:

if(Build.VERSION.SDK_INT>=16 && Build.VERSION.SDK_INT<21){
    getActivity().finishAffinity();
} else if(Build.VERSION.SDK_INT>=21){
    getActivity().finishAndRemoveTask();
}

3.如果你想在非Activity类中退出应用程序,并且不能访问Activity(如Service),我建议你使用BroadcastReceiver。您可以将此方法添加到项目中的所有活动中。

创建LocalBroadcastManager和BroadcastReceiver实例变量。您可以替换getPackageName()+"。Closeapp”如果你想的话。

LocalBroadcastManager mLocalBroadcastManager;
BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(getPackageName()+".closeapp")){
            if(Build.VERSION.SDK_INT>=16 && Build.VERSION.SDK_INT<21){
                finishAffinity();
            } else if(Build.VERSION.SDK_INT>=21){
                finishAndRemoveTask();
            }
        }
    }
};

将这些添加到Activity的onCreate()方法。

mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
IntentFilter mIntentFilter = new IntentFilter();
mIntentFilter.addAction(getPackageName()+".closeapp");
mLocalBroadcastManager.registerReceiver(mBroadcastReceiver, mIntentFilter);

另外,不要忘记在Activity的onDestroy()方法调用unregister receiver

mLocalBroadcastManager.unregisterReceiver(mBroadcastReceiver);

对于退出应用程序,你必须使用LocalBroadcastManager发送广播,我在我的PlayService类中使用,它扩展了服务。

LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(PlayService.this);
localBroadcastManager.sendBroadcast(new Intent(getPackageName() + ".closeapp"));

简单和简单的方法退出应用程序

Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);

从API 16开始,你可以使用finishAffinity方法,它似乎非常接近于通过它的名称和Javadoc描述关闭所有相关的活动:

this.finishAffinity();

Finish this activity as well as all activities immediately below it in the current task that have the same affinity. This is typically used when an application can be launched on to another task (such as from an ACTION_VIEW of a content type it understands) and the user has used the up navigation to switch out of the current task and into its own task. In this case, if the user has navigated down into any other activities of the second application, all of those should be removed from the original task as part of the task switch. Note that this finish does not allow you to deliver results to the previous activity, and an exception will be thrown if you are trying to do so.


从API 21开始,您可以使用非常类似的命令

finishAndRemoveTask();

完成此任务中的所有活动,并将其从最近任务列表中移除。