《第一行代码》 随手笔记——第五章 广播机制


5.2.1动态注册监听网络变化

public class MainActivity extends Activity {
	
	private IntentFilter intentFilter;

	private NetworkChangeReceiver networkChangeReceiver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		intentFilter = new IntentFilter();
		intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
		networkChangeReceiver = new NetworkChangeReceiver();
		registerReceiver(networkChangeReceiver, intentFilter);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(networkChangeReceiver);//动态注册的广播接收器一定都要取消注册
	}

	class NetworkChangeReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			<p><strong><span style="white-space:pre">			</span>ConnectivityManager connectionManager = (ConnectivityManager)</strong></p><p>                    <strong>getSystemService(Context.CONNECTIVITY_SERVICE);</strong></p><p><strong>            NetworkInfo networkInfo =connectionManager.getActiveNetworkInfo();</strong></p><p><strong>            if (networkInfo !=null && networkInfo.isAvailable()) {</strong></p><p><strong>                Toast.makeText(context,"network is available",</strong></p><p><strong>                        Toast.LENGTH_SHORT).show();</strong></p><p><strong>            } else {</strong></p><p><strong>                Toast.makeText(context,"network is unavailable",</strong></p><p><strong>                        Toast.LENGTH_SHORT).show();</strong></p><p><strong>            }</strong></p>
		}

	}

}

AndroidManifest.xml 配置权限

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>


5.2.2 静态注册广播实现开机启动


新建一个BootCompleteReceiver继承自BroadcastReceiver,代码如下所示:

public class BootCompleteReceiver extends BroadcastReceiver {
	
	@Override
	public void onReceive(Context context, Intent intent) {
		Toast.makeText(context, "Boot Complete", Toast.LENGTH_LONG).show();
	}

}
 
 
配置Androidmanifest文件
 
 
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.broadcasttest"
    android:versionCode="1"
    android:versionName="1.0" >
    ……
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
		……
        <receiver android:name=".BootCompleteReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>
</manifest>


5.3 发送有序广播


button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent("com.example.broadcasttest. MY_BROADCAST");
				<strong>sendOrderedBroadcast(intent, null);</strong>
			}
		});

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.broadcasttest"
    android:versionCode="1"
    android:versionName="1.0" >
   ……
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        ……
        <receiver android:name=".MyBroadcastReceiver">
            <strong><intent-filter android:priority="100" ></strong>
                <action android:name="com.example.broadcasttest.MY_BROADCAST"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>

android:priority属性给广播接收器设置了优先级


public class MyBroadcastReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		Toast.makeText(context, "received in MyBroadcastReceive", Toast.LENGTH_SHORT).show();
		abortBroadcast();
	}

}

如果在onReceive()方法中调用了abortBroadcast()方法,就表示将这条广播截断。


5.4 本地广播

public class MainActivity extends Activity {
	
	private IntentFilter intentFilter;

<strong>	private LocalReceiver localReceiver;

	private LocalBroadcastManager localBroadcastManager;</strong>

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		<strong>localBroadcastManager = LocalBroadcastManager.getInstance(this); // 获取实例</strong>
		Button button = (Button) findViewById(R.id.button);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent("com.example.broadcasttest. LOCAL_BROADCAST");
				<strong>localBroadcastManager.sendBroadcast(intent); // 发送本地广播</strong>
			}
		});
		intentFilter = new IntentFilter();
		intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST");
		localReceiver = new LocalReceiver();
		<strong>localBroadcastManager.registerReceiver(localReceiver, intentFilter); // 注册本地广播监听器</strong>
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		<strong>localBroadcastManager.unregisterReceiver(localReceiver);//注销广播</strong>
	}

	class LocalReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			Toast.makeText(context, "received local broadcast", Toast.LENGTH_SHORT).show();
		}

	}

}

本地广播是无法通过静态注册的方式来接收的

使用本地广播的几点优势

1.        可以明确地知道正在发送的广播不会离开我们的程序,因此不需要担心机密数据泄漏的问题。

2.        其他的程序无法将广播发送到我们程序的内部,因此不需要担心会有安全漏洞的隐患。

3.        发送本地广播比起发送系统全局广播将会更加高效。


5.5 广播的最佳实践——实现强制下线


先创建一个ActivityCollector类用于管理所有的活动,代码如下所示:

public class ActivityCollector {
	
	public static List<Activity> activities = new ArrayList<Activity>();

	public static void addActivity(Activity activity) {
		activities.add(activity);
	}

	public static void removeActivity(Activity activity) {
		activities.remove(activity);
	}

	public static void finishAll() {
		for (Activity activity : activities) {
			if (!activity.isFinishing()) {
				activity.finish();
			}
		}
	}

}

创建BaseActivity类作为所有活动的父类

public class BaseActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ActivityCollector.addActivity(this);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		ActivityCollector.removeActivity(this);
	}
	
}


广播接收器


public class ForceOfflineReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(final Context context, Intent intent) {
		AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
		dialogBuilder.setTitle("Warning");
		dialogBuilder.setMessage("You are forced to be offline. Please try to login again.");
		dialogBuilder.setCancelable(false);
		dialogBuilder.setPositiveButton("OK",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						ActivityCollector.finishAll(); // 销毁所有活动
						Intent intent = new Intent(context, LoginActivity.class);
						intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						context.startActivity(intent); // 重新启动LoginActivity
					}
				});
		AlertDialog alertDialog = dialogBuilder.create();
		// 需要设置AlertDialog的类型,保证在广播接收器中可以正常弹出
		alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
		alertDialog.show();
	}

}

首先肯定是使用AlertDialog.Builder来构建一个对话框,注意这里一定要调用setCancelable()方法将对话框设为不可取消,否则用户按一下Back键就可以关闭对话框继续使用程序了。然后使用setPositiveButton()方法来给对话框注册确定按钮,当用户点击了确定按钮时,就调用ActivityCollector的finishAll()方法来销毁掉所有活动,并重新启动LoginActivity这个活动。另外,由于我们是在广播接收器里启动活动的,因此一定要给Intent加入FLAG_ACTIVITY_NEW_TASK这个标志。最后,还需要把对话框的类型设为TYPE_SYSTEM_ALERT,不然它将无法在广播接收器里弹出。


由于我们在ForceOfflineReceiver里弹出了一个系统级别的对话框,因此必须要声明android.permission.SYSTEM_ALERT_WINDOW权限

 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />



你可能感兴趣的:(《第一行代码》 随手笔记——第五章 广播机制)