不少網友可能發現本身的Android程序有不少Activity,好比說主窗口A,調用了子窗口B,在B中如何關閉整個Android應用程序呢? 這裏Android123給你們三種比較簡單的方法實現。java
首先要說明在B中直接使用finish(),接下來手機顯示的仍是主窗口A,因此一塊兒來看看Android開發網是如何實現的吧.android
1. Dalvik VM的本地方法c#
android.os.Process.killProcess(android.os.Process.myPid()) //獲取PID,目前獲取本身的也只有該API,不然從/proc中本身的枚舉其餘進程吧,不過要說明的是,結束其餘進程不必定有權限,否則就亂套了。
System.exit(0); //常規java、c#的標準退出法,返回值爲0表明正常退出app
2. 任務管理器方法ide
首先要說明該方法運行在Android 1.5 API Level爲3以上才能夠,同時須要權限android.permission.RESTART_PACKAGES,咱們直接結束本身的package便可,直接使用ActivityManager類的restartPackage方法便可,參數爲package name,該類經過getSystemService(Context.ACTIVITY_SERVICE)來實例化ActivityManager對象,這種方法系統提供的,但須要顯示聲明權限,因此使用中須要綜合考慮。ui
3. 根據Activity的聲明週期this
咱們知道Android的窗口類提供了歷史棧,咱們能夠經過stack的原理來巧妙的實現,這裏咱們在A窗口打開B窗口時在Intent中直接加入標誌Intent.FLAG_ACTIVITY_CLEAR_TOP,這樣開啓B時將會清除該進程空間的全部Activity。rest
在A窗口中使用下面的代碼調用B窗口對象
Intent intent = new Intent();
intent.setClass(Android123.this, CWJ.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //注意本行的FLAG設置
startActivity(intent);繼承
常常都須要退出一個Android應用程序,若是應用裏Activity不少得話,直接用finish()則只能退出當前的Activity,而不能徹底退出,網上雖有不少實現方法,我這裏也給你們介紹一實現方法。好比如今我有N個Activity,而每個Activity裏面均可以經過菜單鍵來退出應用程序,該怎麼辦呢?是否是每個Activity裏面都寫下菜單呢?固然不須要,咱們能夠定義一個通用的BaseActivity,這個Activity固然繼承最基本的Activity,而咱們在BaseActivity裏面實現菜單的功能,實現程序的退出,而後在程序裏面其餘的Activity來繼承至這個BaseActivity。
代碼以下:
package com.apkstory;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class BaseActivity extends Activity {
public static final String TAG = "BaseActivity" ;
public static ArrayList<Activity> activityList = new ArrayList<Activity>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityList.add(this);
Log.i(TAG, activityList.toString()) ;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "退出程序");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == 0) {
exitApp();
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onDestroy() {
super.onDestroy();
activityList.remove(this);
Log.i(TAG, activityList.toString()) ;
}
/**
* 完徹底全退出應用程序
*/
public void exitApp() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("提示");
builder.setMessage("您肯定要退出程序嗎?");
builder.setPositiveButton("肯定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (activityList.size() > 0) {
for (Activity activity : activityList) {
activity.finish();
}
android.os.Process.killProcess(android.os.Process.myPid());
}
}
});
builder.setNegativeButton("取消", null);
builder.show();
}
}
import java.util.LinkedList;
import java.util.List;
import android.app.Activity;
import android.app.Application;
public class ExitApplication extends Application {
private List<Activity> activityList=new LinkedList<Activity>();
private static ExitApplication instance;
private ExitApplication()
{
}
//單例模式中獲取惟一的ExitApplication 實例
public static ExitApplication getInstance()
{
if(null == instance)
{
instance = new ExitApplication();
}
return instance;
}
//添加Activity 到容器中
public void addActivity(Activity activity)
{
activityList.add(activity);
}
//遍歷全部Activity 並finish
public void exit()
{
for(Activity activity:activityList)
{
activity.finish();
}
System.exit(0);
}
}
下面的三個類IndexActivity, BActivity,CActivity 是簡單的例子, 分別是
IndexActivity–>BActivity–>CActivity 的跳轉順序。在每一個Activity 類中onCreate()方法中調
用ExitApplication.getInstance().addActivity(Activity activity)方法。在任何一個Activity 界面退
出應用程序時,只要調用ExitApplication.getInstance().exit()方法,就能夠在任何一個Activity
中徹底退出應用程序。
IndexActivity 類源代碼:
view plaincopy to clipboardprint?
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class IndexActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next=(Button)findViewById(R.id.next_to_b);
next.setOnClickListener(nextClick);
Button exit=(Button)findViewById(R.id.exit_main);
exit.setOnClickListener(exitClick);
ExitApplication.getInstance().addActivity(this);
}
OnClickListener nextClick=new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(IndexActivity.this,BActivity.class);
startActivity(intent);
}
};
OnClickListener exitClick=new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ExitApplication.getInstance().exit();
}
};
}
BActivity 類源代碼:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class BActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.b);
Button next_to_c=(Button)findViewById(R.id.next_to_c);
next_to_c.setOnClickListener(next_to_cClick);
Button exit_b=(Button)findViewById(R.id.exit_b);
exit_b.setOnClickListener(exitClick);
ExitApplication.getInstance().addActivity(this);
}
OnClickListener next_to_cClick=new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(BActivity.this,CActivity.class);
startActivity(intent);
}
};
OnClickListener exitClick=new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ExitApplication.getInstance().exit();
}
};
}
CActivity 類源代碼:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class CActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.c);
Button exit_c=(Button)findViewById(R.id.exit_c);
exit_c.setOnClickListener(exitClick);
ExitApplication.getInstance().addActivity(this);
}
OnClickListener exitClick=new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ExitApplication.getInstance().exit();
//若是隻是調用如下其中的一個方法,並不會徹底退出應用
//android.os.Process.killProcess(android.os.Process.myPid());
//System.exit(0);
}
};
}