如何退出整個Android 應用

通過System.exit(0)finish()以及返回鍵,只能結束當前的Activity,當我們打開多個Activity並需要直接退出整個Android應用時,需要多次單擊back返回鍵,方能退出。給用戶體驗不是很好。下面我們來介紹幾種直接退出整個Android應用的方法。

解決方案一:

創建一個輔助類,用於保存所有的已打開的Activity,當打開一個Activity時,就將其添加到已打開的Activity集合中(通常是onCreate()方法中調用add方法),當關閉一個Activity時,需要在集合中刪除該Activity通常是onDestroy()方法中調用delete方法)。關鍵代碼如下:

public class ActivityMgr extends Application {

private static ActivityMgr activityMgr = null;

public List<Activity> activities = new LinkedList<Activity>();

public synchronized static ActivityMgr getInstance() {

if (null == activityMgr) {

activityMgr = new ActivityMgr();

}

return activityMgr;

}

public void addActivity(Activity activity) {

if (activity != null) {

activities.add(activity);

}

}

public void exit() {

for (Activity activity : activities) {

System.out.println("Activity="+activity);

if (activity != null) {

activity.finish();

}

}

System.exit(0);

}

public void delete(Activity activity){

if(activities.contains(activity)){

activities.remove(activity);

System.out.println("Delete!");

}

}

public void onLowMemory() {

super.onLowMemory();

System.gc();

}

}

ActivityonCreate()方法中將Activity本身添加到集合中的語句如下:

ActivityMgr.getInstance().addActivity(this);

ActivityonDestroy()方法中,將Activity從集合中刪除的語句如下:

protected void onDestroy() {

ActivityMgr.getInstance().delete(this);

super.onDestroy();

}

解決方案二:

通過發送廣播的方式,通知所有的Activity進行關閉,具體做法創建一個自定義的MyActivity讓其繼承於Activity,在該Activity中定義一個內部廣播接收器類,然後在onResume()方法中進行動態註冊廣播接收器。最後讓其他的Activity繼承於MyActivity而不是系統的Activity,這樣所有的Activity類都繼承了MyActivity中的onResume()方法,也就註冊了廣播接收器,當需要退出應用程序時,只需要發送一個廣播即可,這時所有的Activity的內部廣播接收器都可以接收到該廣播,然後執行finish()方法,結束Activity本身。

public class MyActivity extends Activity {

ExitBroadcastReceiver exitReceiver;

private class ExitBroadcastReceiver extends BroadcastReceiver{

public void onReceive(Context context, Intent intent) {

finish();//結束當前的Activity

unregisterReceiver(exitReceiver);//取消註冊

}

}

protected void onResume() {

exitReceiver=new ExitBroadcastReceiver();//創建廣播接收器

IntentFilter filter=new IntentFilter("iet.jxufe.cn.android.exit");//過濾條件

registerReceiver(exitReceiver, filter);//註冊廣播接收器

super.onResume();

}

}

需要退出時,只需要發送廣播即可。代碼如下:

Intent intent=new Intent();

intent.setAction("iet.jxufe.cn.android.exit");//設置接收廣播的條件

sendBroadcast(intent);//發送廣播

解決方案三:

通過Activity的啓動模式來實現該功能,Activity的啓動模式主要有以下幾種:

1standard模式;也就是默認模式,每次激活Activity時都會創建一個新的Activity實例,並放入任務棧中。

2singleTop模式;如果在任務棧中的棧頂存在該Activity實例,下次激活該Activity實例時就不會創建新的 Activity的實例,直接重用它(在重用的這個過程中會調用實例的OnNewIntent()這個方法),否則就創建新的Activity實例。

3singleTask模式;如果在棧中已經有該Activity的實例,以後就不會創建新的實例了,而會重用該實例(在重用的這個過程中會調用實例的OnNewIntent()這個方法)。重用時,如果該Activity實例不是在棧頂,它會讓該實例回到棧頂,而它上面的實例將會被移出棧。如果棧中不存在該實例,將會創建新的實例放入棧中。

4singleInstance模式;在一個新棧中創建該Activity的實例,並讓多個應用共享該棧中的該Activity實例。一旦該模式的Activity實例已經存在於某個棧中,任何應用再激活該Activity時都會重用該棧中的實例( 會調用實例的 onNewIntent() )。其效果相當於多個應用共享一個應用,不管誰激活該 Activity 都會進入同一個應用中。

在這裏我們可以把主activity設置爲singleTask模式,當我們想退出整個應用時,就可以通過intent打開該activity,然後系統會把它之上的activity移出activity棧,然後我們再在該activityonNewIntent方法進行finish,就可以達到退出該應用程序的目的。

該方案有一定的侷限性,僅適應於有一個固定的Activity作爲棧底的情況,如果棧底元素不是固定的,則有可能達不到該效果。例如若此時棧中並不存在該實例對象,則會創建一個新的對象,也就達不到關閉其他Activity的目的。

Activity的啓動模式可以在清單文件中進行配置,例如:

<activity

    android:name="MainActivity"

    android:label="@string/title_activity_main"

    android:launchMode="singleTask" >

   <intent-filter>

       <action android:name="android.intent.action.MAIN" />

       <category android:name="android.intent.category.LAUNCHER" />

   </intent-filter>

</activity>

MainActivity中重寫onNewIntent()方法,執行finish()方法,代碼如下:

protected void onNewIntent(Intent intent) {

super.onNewIntent(intent);

this.finish();

}

在需要退出整個應用時,創建一個Intent,跳轉到MainActivity即可,此時由於MainActivity處於棧底,它上面的所有的Activity都會銷燬,並會調用MainActivityonNewIntent()方法。


最後介紹一種,Android徹底關閉當前應用(2.2版本不再有效)

以下方法用於關閉當前應用(此方法一般不建議使用,因爲採用殺死進程的方法會導致activity所在進程被殺死,使得activity處於界面可見,但是無法響應事件,不可操作狀態,也無法將activity正常結束的情況)

ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
manager.restartPackage(getPackageName());

需要權限:
"android.permission.RESTART_PACKAGES"

函數說明:
void android.app.ActivityManager.restartPackage(String packageName)

public void restartPackage (String packageName)
Since: API Level 3

Have the system perform a force stop of everything associated with the given application package. All processes that share its uid will be killed, all services it has running stopped, all activities removed, etc. In addition, a ACTION_PACKAGE_RESTARTED broadcast will be sent, so that any of its registered alarms can be stopped, notifications removed, etc.

You must hold the permission RESTART_PACKAGES to be able to call this method.
Parameters
packageName     The name of the package to be stopped.

與當前應用相關的應用、進程、服務等也會被關閉。
會發送 ACTION_PACKAGE_RESTARTED廣播。
不要被函數名誤導。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章