当用户从主屏幕按下返回键时,我想结束我的应用程序。我尝试使用System.exit(0)
,但它似乎不工作。
我要怎么做才能手动结束我的应用程序?
package com.mahavega.qcdemo;
import com.mahavega.qcdemo.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener {
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gestureDetector = new GestureDetector(this, new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
ImageView ad = (ImageView) findViewById(R.id.imageView1);
ImageView im2 = (ImageView) findViewById(R.id.imageView2);
ImageView im3 = (ImageView) findViewById(R.id.imageView3);
ImageView im4 = (ImageView) findViewById(R.id.imageView4);
ImageView im5 = (ImageView) findViewById(R.id.imageView5);
ad.setOnClickListener(MainActivity.this);
im2.setOnTouchListener(gestureListener);
im2.setOnClickListener(MainActivity.this);
im3.setOnTouchListener(gestureListener);
im3.setOnClickListener(MainActivity.this);
im4.setOnTouchListener(gestureListener);
im4.setOnClickListener(MainActivity.this);
im5.setOnTouchListener(gestureListener);
im5.setOnClickListener(MainActivity.this);
ad.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Adds.class));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
float sensitvity = 50;
if((e1.getX() - e2.getX()) < sensitvity){
startActivity(new Intent(MainActivity.this, Login.class));
}
return super.onFling(e1, e2, velocityX, velocityY);
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
System.exit(0);
finish();
super.onBackPressed();
}
}
这是我的全部代码
根据你运行/构建的Android版本,你可能需要给你的Activity
添加一点帮助来捕捉后退按钮。添加这个函数可以提供以下帮助:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (Integer.parseInt(android.os.Build.VERSION.SDK) < 5
&& keyCode == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount() == 0) {
onBackPressed();
}
return true;
}
在您的Activity
呼叫finish()
。此处参考https://developer.android.com/reference/android/app/Activity.html#finish()
public static final void android.os.Process.killProcess(int pid)
简单,工作和做你想要的。但请注意,不建议在Android上显式退出应用程序。(普通的GUI应用程序只是在最上面的Activity上调用finish(),并让操作系统决定何时退出应用程序进程。)
调用moveTaskToBack(true)在你的Activity(它退出你的应用程序,但不销毁它)