参考地址:http://www.cnblogs.com/ada-zheng/archive/2013/02/28/2936890.html
项目中有时要用到点击两次提示用户退出的操作。一般都是在程序的入口Activity中提示。
有时程序中也会有好几层的activity,想从最里边的一层调出的需求,上网查找资料,并经过好长时间测试,才调试成功。直接上代码:
public class MyApplication extends Application {
private static MyApplication instance;
private List<Activity> activities =new ArrayList<Activity>();
public MyApplication(){ } //单例模式中获取唯一的application
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
public static MyApplication getInstance()
{
if(null==instance)
{
instance=new MyApplication();
}
return instance;
} //存放Activity到list中
public void addActivity(Activity activity){ activities.add(activity); }
@Override //遍历存放在list中的Activity并退出
public void onTerminate()
{
super.onTerminate();
for(Activity activity : activities){
activity.finish();
}
android.os.Process.killProcess(android.os.Process.myPid());
}
}
public class MainActivity extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyApplication.getInstance().addActivity(this);
tv = (TextView) findViewById(R.id.main);
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
});
}
private static boolean mBackKeyPressed = false;//记录是否有首次按键
@Override
public void onBackPressed() {
if(!mBackKeyPressed){
Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
mBackKeyPressed = true;
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
mBackKeyPressed = false;
}//延时两秒,如果超出则擦错第一次按键记录
}, 2000);
}
else{
MyApplication.getInstance().onTerminate();
}
}
}
public class SecondActivity extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
tv = (TextView) findViewById(R.id.second);
MyApplication.getInstance().addActivity(this);
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(SecondActivity.this,ThirdActivity.class);
startActivity(intent);
}
});
}
private static boolean mBackKeyPressed = false;//记录是否有首次按键
@Override
public void onBackPressed() {
if(!mBackKeyPressed){
Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
mBackKeyPressed = true;
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
mBackKeyPressed = false;
}//延时两秒,如果超出则擦错第一次按键记录
}, 2000);
}
else{
MyApplication.getInstance().onTerminate();
}
}
}
<application
android:name="com.example.doubleclickdemo.MyApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.doubleclickdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.doubleclickdemo.SecondActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.doubleclickdemo.ThirdActivity"
android:label="@string/app_name" >
</activity>
</application>