按下后退出应用程序

时间:2013-08-05 13:25:29

标签: android exit

对不起再次按钮我在这里找出正确的位置。我只是想在这个类上只有在退回时退出应用程序,但我无法弄清楚究竟要写什么或在哪里写它。谢谢您的帮助。下面的课程代码。     package com.example.whattodo2;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class Title extends Activity {
Button reset, rts;
ImageView title;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_title);
    reset = (Button) findViewById(R.id.reset);
    reset.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                       double rand = Math.random();
                       if(rand < 0.5){
                           Intent reset1 = new Intent(Title.this, MainActivity.class);
                           startActivity(reset1);
                       } else {
                           Intent reset2 = new Intent(Title.this, Question36.class);
                           startActivity(reset2);
                       }
                }
            });
    rts = (Button) findViewById(R.id.rts);
    rts.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Intent rts=new Intent(Title.this,Rts.class);
                    startActivity(rts);

                }
            });

         final Animation a = AnimationUtils.loadAnimation(this, R.animator.animation);
         a.reset();
         final ImageView rImage = (ImageView) findViewById(R.id.title);

         RelativeLayout layout = (RelativeLayout) findViewById(R.id.root);
         layout.setOnClickListener(new OnClickListener() {

             @Override public void onClick(View v) {
         rImage.startAnimation(a);
         func(); //A call to the function.

         }
         });


}

protected void func() {
    // TODO Auto-generated method stub

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.title, menu);
    return true;
}

}

2 个答案:

答案 0 :(得分:2)

像这样使用:

public class Title extends Activity {

@Override
public void onBackPressed() {
// TODO Auto-generated method stub

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

super.onBackPressed();
}
}

希望这会给你一些解决方案。

答案 1 :(得分:1)

在onCreate之前进行所有活动:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {

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

            // finish();
    }
    return true;
}