双击返回键退出

package com.bwie.back;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
/**
 * 按两次返回键退出
 * @author zhang
 *
 */
public class MainActivity extends Activity {


private long time=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//按返回键事件
@Override
public void onBackPressed(){
if(time<=0){
Toast.makeText(this,"再按一次退出",0).show();
time = System.currentTimeMillis();
}else{
long currentTimeMillis2 = System.currentTimeMillis();
if(currentTimeMillis2-time<1000){
finish();
}else{
Toast.makeText(this,"再按一次退出",0).show();
time=currentTimeMillis2;
}
}
// super.onBackPressed();
}
}

你可能感兴趣的:(双击返回键退出)