BaseActivity如下:
package cc.c; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; public class BaseActivity extends Activity { private BroadcastReceiver mBroadcastReceiver=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //注册广播 initBroadcastReceiver(); } private void initBroadcastReceiver(){ mBroadcastReceiver=new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { finish(); } }; IntentFilter intentFilter=new IntentFilter("cc.finish"); registerReceiver(mBroadcastReceiver, intentFilter); } @Override protected void onDestroy() { super.onDestroy(); //取消广播 destroyBroadcastReceiver(); } private void destroyBroadcastReceiver(){ if (mBroadcastReceiver!=null) { unregisterReceiver(mBroadcastReceiver); } } }
MainActivity如下:
package cc.c; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; /** * Demo描述: * 有时需要在某个时刻完全退出应用即finish掉所有的Activity. * * 解决方式: * 1 编写BaseActivity * 2 在BaseActivity中注册广播接收者,在onReceive()中执行finish() * 3 App中Activity均继承自该BaseActivity. * * 当在某个时刻需要退出应用时,发出一个广播即可. * * */ public class MainActivity extends BaseActivity { private Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mContext=this; Button button=(Button) findViewById(R.id.mainActivityButton); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(mContext, OtherActivity.class); startActivity(intent); } }); } @Override protected void onDestroy() { System.out.println("MainActivity finish"); super.onDestroy(); } }
OtherActivity如下:
package cc.c; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class OtherActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.other); init(); } private void init(){ Button button=(Button) findViewById(R.id.otherActivityButton); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { //发送广播,达到退出应用的目的 Intent intent=new Intent(); intent.setAction("cc.finish"); sendBroadcast(intent); } }); } @Override protected void onDestroy() { System.out.println("OtherActivity finish"); super.onDestroy(); } }
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/mainActivityButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="跳转到其余Activity" /> </RelativeLayout>
other.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/otherActivityButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="点击后退出应用" /> </RelativeLayout>
AndroidManifest.xml如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cc.c" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="10" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="cc.c.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="cc.c.OtherActivity"></activity> </application> </manifest>