1.ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
manager.killBackgroundProcesses(package);
不行
2.android.os.Process.killProcess(android.os.Process.myPid());
也不行
3.manager.restartPackage(package);
还是不行
4.
Intent MyIntent = new Intent(Intent.ACTION_MAIN);
MyIntent.addCategory(Intent.CATEGORY_HOME);
startActivity(MyIntent);
finish();
这个只是退回到桌面,如果打开多个Activity关闭重新打开也会有问题,还是还是不行
看到有说广播机制,发现是个好东东,能彻底解决这个问题,废话不说看代码:
public abstract class EnterActivity extends BaseActivity { ... // 写一个广播的内部类,当收到动作时,结束activity private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { unregisterReceiver(this); // 这句话必须要写要不会报错,不写虽然能关闭,会报一堆错 ((Activity) context).finish(); } }; @Override public void onResume() { super.onResume(); // 在当前的activity中注册广播 IntentFilter filter = new IntentFilter(); filter.addAction(Attribute.PAGENAME); registerReceiver(this.broadcastReceiver, filter); // 注册 } /** * 关闭 */ public void close() { Intent intent = new Intent(); intent.setAction(Attribute.PAGENAME); // 说明动作 sendBroadcast(intent);// 该函数用于发送广播 finish(); } ... }