android应用程序以编程方式关闭

时间:2014-03-27 05:50:39

标签: android exit

我正在开发一个Android项目,要求是在任何活动中发生异常。然后应用程序应在执行catch块后自动退出。我知道Android应用程序架构并不建议自行关闭应用程序。

4 个答案:

答案 0 :(得分:1)

使用它来编写应用程序

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

请查看以下链接了解更多详情Android App closing 希望它有所帮助,谢谢:)。

答案 1 :(得分:1)

 Intent intObj=new Intent(this, Home.class);
 intObj.putExtra("finish", true);
 intObj.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
              Intent.FLAG_ACTIVITY_CLEAR_TASK |
               Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(intObj);

答案 2 :(得分:0)

要以编程方式(并优雅地)终止您的Android应用程序,请考虑实施完成链机制。具体来说,

(1)实现所有Activity子类的onResume(),如下所示。

protected void onResume()
{
    super.onResume();

    // Check if the application is in the process of
    // stopping. How to implement 'App' in the code
    // below is up to you.
    if (App.getInstance().isStopping())
    {
        finish();
        return;
    }

    ......

(2)如果要终止申请,请调用以下方法。

protected void exit()
{
    // Turn into 'stopping' state.
    App.getInstance().setStopping(true);

    // Finish this Activity. This removes this Activity
    // instance from the Activity stack and the next
    // Activity instance will be displayed. And if the
    // next Activity's onResume() is implemented like the
    // above example, the next Activity will finish(), too.
    // This finish-chain mechanism continues until it
    // reaches the root Activity.
    finish();
}

因为Android可能会重复使用根Activity实例,而不是“' new'它,root Activity的onResume()必须清除'停止'标志。

protected final void onResume()
{
    super.onResume();

    if (App.getInstance().isStopping())
    {
        // Close this Activity, meaning that this application is closed
        // because this Activity is the root.
        finish();

        // Reset the termination state because Android may reuse this
        // Activity instance instead of creating a new one.
        App.getInstance().setStopping(false);
    }
    else
    {
        ......
    }
}

实施'退出'的示例应用程序(甚至'重启')机制:
https://github.com/TakahikoKawasaki/nv-android-base-sample

示例基础活动:
https://github.com/TakahikoKawasaki/nv-android-base/blob/master/src/main/java/com/neovisionaries/android/app/BaseRootActivity.java

答案 3 :(得分:0)

如果您有多个活动,可以使用finishAffinity关闭所有活动

histogram( ~ rt | pp,layout=c(6,4),data = data.frame,
       main=list(
         label="RT distribution per subject",
         cex=1.5),
       xlab=list(
         label="RT (s)",
         cex=0.75),
       ylab=list(
         label="Percentage occurence",
         cex=1.2),
       xlim=c(0,40),
       breaks = 10
)