尝试过System.exit(0)和android.os.Process.killProcess(android.os.Process.myPid())两种方法都失败了。这两个应该均只能退出单activity
最后终于找到一个相对靠谱的方法,即重写activity,通过broadcast的方法解决这个问题。方法如下:
- public class BaseActivity extends Activity {
- public Activity aty;
- public ExitAllBroadCast exitAllBroadCast;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- aty = BaseActivity.this;
- exitAllBroadCast = new ExitAllBroadCast();
- }
- /* (non-Javadoc)
- * @see android.app.Activity#onStart()
- */
- @Override
- protected void onStart() {
- IntentFilter filter = new IntentFilter();// 创建IntentFilter对象
- filter.addAction("com.kg.exit.broadcast");
- registerReceiver(exitAllBroadCast, filter);// 注册Broadcast Receiver
- super.onStart();
- }
- class ExitAllBroadCast extends BroadcastReceiver{
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO Auto-generated method stub
- aty.finish();
- }
- }
- @Override
- protected void onDestroy() {
- // TODO Auto-generated method stub
- super.onDestroy();
- unregisterReceiver(exitAllBroadCast);
- }
- }
直接贴过来了,需要调用退出的部分:
- Intent myIntent = new Intent();// 创建Intent对象
- myIntent.setAction("com.kg.exit.broadcast");
- sendBroadcast(myIntent);// 发送广播
即可。有关全局变量,我只能在退出时调用OnCreate方法恢复初始值,其他的暂时还没有找到更好的方法。
转载自http://blog.csdn.net/kingsonl/article/details/7577058