Android应用程序退出实例
1、Application当中代码:
[java] public class WeiboApplication extends Application {
// 程序退出标记
private List<Activity> activities = new ArrayList<Activity>();
public void addActivity(Activity activity) {
activities.add(activity);
}
@Override
public void onTerminate() {
super.onTerminate();
for (Activity activity : activities) {
activity.finish();
}
android.os.Process.killProcess(android.os.Process.myPid());
}
}
public class WeiboApplication extends Application {
// 程序退出标记
private List<Activity> activities = new ArrayList<Activity>();
public void addActivity(Activity activity) {
activities.add(activity);
}
@Override
public void onTerminate() {
super.onTerminate();
for (Activity activity : activities) {
activity.finish();
}
android.os.Process.killProcess(android.os.Process.myPid());
}
}
2、每个Activity在oncreate当中的代码:
[java]@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main_activity);
WeiboApplication app = (WeiboApplication) getApplication();
app.addActivity(this);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main_activity);
WeiboApplication app = (WeiboApplication) getApplication();
app.addActivity(this);
}
3、在TabActivity当中设置退出代码(类似的可以在普通Activity当中设置)
[java] @Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
// 创建对话框 www.zzzyk.com
LayoutInflater li = LayoutInflater.from(this);
View exitV = li.inflate(R.layout.exitdialog, null);
AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setView(exitV);// 设定对话框显示的View对象
ab.setPositiveButton(R.string.menu_logout_label, new OnClickListener() {
public void onClick(DialogInte易做图ce arg0, int arg1) {
// TODO Auto-generated method stub
WeiboApplication app = (WeiboApplication) getApplication();
app.onTerminate();
}
});
ab.setNegativeButton(R.string.cancel_button_title, null);
// 显示对话框
ab.show();
return false;
}
return false;
};
补充:移动开发 , Android ,