【问题标题】:Automatically closing of an Android app with help of an Intent?在 Intent 的帮助下自动关闭 Android 应用程序?
【发布时间】:2013-10-09 05:17:04
【问题描述】:

我在 Drawables 和 Intent 的帮助下制作了一个简单的初学者应用程序。这个想法正在制作四个活动并使用线程从一个活动移动到其他活动,当它到达最后一个活动时,我想自动关闭应用程序,弹出一个祝酒词“再见”或其他东西。 如果可以通过 Intent 或任何其他方式实现,请指导我。

我的最后一个活动的代码如下,但它循环到最后一个活动到 Infinity:

package mj.manan.thisisit;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class ScreenLast extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screenlast);

        Thread t1 = new Thread(){
            public void run(){

                try {
                    Thread.sleep(2000);
                    finish();
                } catch (InterruptedException e) {
                    // Auto-generated catch block
                    e.printStackTrace();
                }finally{
                     Intent innt = new Intent(ScreenLast.this,ScreenLast.class);
                     innt.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                     startActivity(innt);
                     ScreenLast.this.finish();

                }
            }
        };
        t1.start();

 }

}

【问题讨论】:

    标签: android android-intent android-activity undefined-behavior exit-code


    【解决方案1】:

    退出intent 的应用程序,请使用以下代码:

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    startActivity(intent);
    

    试试看。

    【讨论】:

    【解决方案2】:

    可以有不同的方法。我认为您也可以为您的情况使用广播发送器和接收器。我发布过的类似答案可能对您有所帮助。

    Finish an activity from another activity

    【讨论】:

      【解决方案3】:

      如果我的理解是正确的,您想在 Activity 堆栈不为空时终止您的 Activity,或者更准确地说,堆栈中有多个 Activity。

      我会告诉你我是怎么做到的,你可以随意调整它

      当您到达要关闭活动的点时,请执行以下操作:

                         Intent intent = new Intent(Main.this, Main.class);
                              intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                              intent.putExtra("EXIT", true);
                              startActivity(intent);
      

      然后在 onCreate 中添加:

      if (getIntent().getBooleanExtra("EXIT", false)) {
                  finish();
              }
      

      您可以将吐司添加到第二部分或添加计时器或您喜欢的任何内容。 关键是,首先我们清除所有的 Activity 堆栈,只留下我们当前的......然后我们完成它。

      【讨论】:

      • 它做了一个循环,并没有退出应用程序。
      • 向我解释一下,如果你清除所有堆栈,它怎么会形成循环?...所以阅读代码...理解它并使用它...我一直在使用它.. . 它在我的应用程序中运行 ...
      【解决方案4】:

      您可以使用此示例代码退出应用程序

                              Intent intent1 = new Intent(Intent.ACTION_MAIN);
                              intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                              intent1.addCategory(Intent.CATEGORY_HOME);
                              startActivity(intent1);
      

      【讨论】:

        【解决方案5】:

        您正在调用相同的活动:- Intent innt = new Intent(ScreenLast.this,ScreenLast.class);

        要么调用其他活动而不是这个。

        或者

        您也可以为此使用处理程序-

        @Override
            protected void onCreate(Bundle savedInstanceState) 
                {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_screenlast);
                Toast.makeText(ScreenLast.this, "Bye", Toast.LENGTH_SHORT).show();
                move_next();
                }
        
        public void move_next()
        {
        final Handler handle = new Handler();
                Runnable delay = new Runnable() {
                    public void run() 
                    {               
                        finish();
                    }
                };
                handle.postDelayed(delay,2000);
        }
        

        【讨论】:

        • 我只是想退出应用程序,而不是重复它
        • 如果你想运行一次该活动,那么你必须调用finish();并最终删除所有内容。
        • 尝试使用处理程序的代码。在您调用下一个活动时的其他 3 个活动中,即 Intent i = new Intent(this, next.class);开始活动(一);只需添加完成();在这段代码之后。
        猜你喜欢
        • 2011-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-18
        • 2013-05-01
        • 2017-02-02
        相关资源
        最近更新 更多