第一种方式:
android.os.Process.killProcess(android.os.Process.myPid());获取PID System.exit(0);//返回0表示正常退出
附:我使用时,发现退出不太友好,屏幕有黑屏的一瞬间。
第二种方式:
ActivityManager am= (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); am.restartPackage(getPackageName());
注:系统会将该包下的所有进程,服务全部杀掉,需要加上以下权限:<uses-permission android:name="android.permission.RESTART_PACKAGES" />第三种方式:发广播,例子如下:AActivity:public class MainActivity extends Activity { String AF="ACTIVITY_FININSH"; MyReceiver mFReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mFReceiver = new MyReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(AF); registerReceiver(mFReceiver,filter); } public void doGet(View v){ Intent intent = new Intent(MainActivity.this,BActivity.class); startActivity(intent); } class MyReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { if(AF.equals(intent.getAction())){ Log.d("TAG","cccc AAAAA==>>>>> jjjjjjjjj"); finish(); } } }; @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(mFReceiver); } }BActivity:public class BActivity extends Activity { String AF="ACTIVITY_FININSH"; MyReceiver mFReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_b); mFReceiver = new MyReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(AF); registerReceiver(mFReceiver,filter); } public void doGet(View v){ Intent intent = new Intent(BActivity.this,CActivity.class); startActivity(intent); android.os.Process.killProcess(android.os.Process.myPid()); System.exit(0); ActivityManager am= (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); am.restartPackage(getPackageName()); } class MyReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { if(AF.equals(intent.getAction())){ Log.d("TAG","cccc BBBBB==>>>>>"); finish(); } } }; @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(mFReceiver); } }CActivity:public class CActivity extends AppCompatActivity { String AF="ACTIVITY_FININSH"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_c); } public void doGet(View v) { //发送关闭Activity广播 getApplicationContext().sendBroadcast(new Intent(AF)); finish(); } }