您的位置:首页 > 移动开发

关于Android退出程序的问题(在主页面或任意页面退出App)

2016-11-23 15:29 507 查看
在写Android退出App时,要注意是在App的主页面退出,还是任意页面退出,不要盲目的进行编码。
如点击App的“退出”按钮,页面跳转到登录页面,在登录页面退出App。在最初直接使用以下:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
MyApplicatation.pool.shutdown();// 关闭线程池                   android.os.Process.killProcess(android.os.Process.myPid());

注意:在登录页面退出时,会从登录页面跳转到主页面,因此,不能直接以此方法。下面记录两种比较好用的退出方式。


1、以广播的方式

在BaseActivity(基类Activity)中
/**
* 自定义广播接收者 ,以此来退出所有的Activity
*/
private BroadcastReceiver exitAppReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {

if ("exitApp".equals(intent.getAction())) {
context.unregisterReceiver(this);
finish();
}

}
};
private IntentFilter filter;
if (filter == null) {
filter = new IntentFilter();
filter.addAction("exitApp");
registerReceiver(exitAppReceiver, filter);
}
在要退出的页面发送广播
Intent intent = new Intent();
intent.setAction("exitApp");
sendBroadcast(intent);


2、自定义AppLication来管理所有的Activity

在自定义的Application中,
// 保存所有的Activity
private List<Activity> activityList;

/**
* 添加activity到activityList集合中
*
* @param activity
*            每一個activity
*/
public void addActivity(Activity activity) {
if (activityList == null) {
activityList = new ArrayList<Activity>();
}
activityList.add(activity);
}

public int getListSize() {
if (activityList != null) {
return activityList.size();
}
return 0;
}

public void removeActivity(Activity activity) {
if (activityList != null) {

if (activityList.contains(activity)) {
activityList.remove(activity);
}
}

}

/** 清空列表,取消引用 */
public void clearActivity() {
activityList.clear();
}

/**
* app退出
*/
public void exit() {
for (Activity activity : activityList) {
if (!activity.isFinishing() && activity != null) {
activity.finish();
}
}
clearActivity();
System.exit(0);
}

/**
* 结束指定类名的Activity
*
* @param cls
*/
public void finishActivity(Activity activity) {

if (activity != null) {
activityList.remove(activity);
activity.finish();
activity = null;
}

}

/**
* 结束指定类名的Activity
*
* @param cls
*/
public void finishActivity(Class<?> cls) {
for (int i = 0; i < activityList.size(); i++) {

if (activityList.get(i).getClass().equals(cls)) {
finishActivity(activityList.get(i));
}
}

}
在基类Activity(BaseActivity)中,
protected MyApplicatation application;
if (application == null) {
application = (MyApplicatation) getApplicationContext();
}
application.addActivity(this);


在退出页面:

if (null != application) {

application.exit();

}

双击退出程序

/**

* 对系统按键操作进行处理,双击退出

*/

@Override

public boolean dispatchKeyEvent(KeyEvent event) {

if (event.getAction() == KeyEvent.ACTION_DOWN) {

// 按下的如果是BACK

if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {

back++;

if (back == 1) {

String appName = getResources()

.getString(R.string.app_name);

getToastdialog(“再按一次退出”);

// Toast.makeText(this, “再按一次退出” + appName, 1000).show();

new Handler().postDelayed(new Runnable() {

public void run() {

back = 0;

}

}, 2000);

}

if (back == 2) {

// 退出整个应用

not = false;

logOut();

}
return false;
}
return super.dispatchKeyEvent(event);
}
return super.dispatchKeyEvent(event);
}

/**
* 用户下线通知服务器
*/
private void logOut() {
new Thread() {
public void run() {
if (null != application) {
application.exit();
}

};
}.start();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 退出App
相关文章推荐