Android 中LocalBroadcastManager的使用方式
Android中BroadcastReceiver主要用途有
发送通知,更新UI或者数据,应用程序间相互通信,监听系统状态(比如开机,网络等)
Android中BroadcasetReceiver的注册方式
manifest清单文件中的全局注册
按照生命周期,在Service或者Activity中使用代码注册
manifest的注册方式
<receiver android:name="com.sample.test.MyBroadcastReciever"> <intent-filter> <action android:name="com.sample.test.ACTION_DO_SOMETHING"></action> <action android:name="android.intent.ACTION_WIFI_STATE_CHANGED"></action> </intent-filter> </receiver>
使用代码注册
SampleActivity
private MyReceiver receiver; @Override public void onStart() { super.onStart(); receiver = new MyReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction("android.intent.action.MY_BROADCAST"); registerReceiver(receiver, filter); } @Override public void onStop(){ super.onStop(); unregisterReceiver(receiver); }
Android中发送广播的方式
普通广播:无论优先级大小,将发送给所有监听Action="com.test.sample.action"的广播,内容不可被修改,无传递性。
Intent intent = new Intent( "com.test.sample.action"); sendBroadcast(intent);
异步(黏性)广播: 当处理完之后的Intent ,依然存在,这时候registerReceiver(BroadcastReceiver, IntentFilter) 还能收到他的值,直到你把它去掉 , 无传递性 , 无法终止(abort())广播。
发这个广播需要权限<uses-permission android:name="android.permission.BROADCAST_STICKY" />
去掉是用这个方法removeStickyBroadcast(intent); 但别忘了在执行这个方法的应用里面 AndroidManifest.xml 同样要加上面的权限;
sendStickyOrderedBroadcast(intent, resultReceiver, scheduler, initialCode, initialData, initialExtras)
有序广播:
按照接收者的优先级顺序接收广播 , 优先级别在 intent-filter 中的 priority 中声明 ,-1000 到1000 之间 ,值越大 优先级越高 。可以终止广播意图的继续传播 , 接收者可以篡改内容,具有传递性。
sendBroadcast(intent);
Android中的BroadcastReceiver可以用来发送信息到另一个广播,这种方式可实现程序或者进程间的通行。
上面回顾了一下Android的广播用例,总体来说安全性都不太好,因此只适用于安全性较低的数据传递,或者页面更新。
在android-support-v4.jar中引入了LocalBroadcastManager,称为局部通知管理器,这种通知的好处是安全性高,效率也高,适合局部通信,可以用来代替Handler更新UI
public class LocalServiceBroadcasterActivity extends Activity { static final String ACTION_STARTED = "com.example.android.supportv4.STARTED"; static final String ACTION_UPDATE = "com.example.android.supportv4.UPDATE"; static final String ACTION_STOPPED = "com.example.android.supportv4.STOPPED"; LocalBroadcastManager mLocalBroadcastManager; BroadcastReceiver mReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final TextView callbackData = (TextView) findViewById(R.id.callback); callbackData.setText("No broadcast received yet"); mLocalBroadcastManager = LocalBroadcastManager.getInstance(this); IntentFilter filter = new IntentFilter(); filter.addAction(ACTION_STARTED); filter.addAction(ACTION_UPDATE); filter.addAction(ACTION_STOPPED); mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(ACTION_STARTED)) { callbackData.setText("STARTED"); } else if (intent.getAction().equals(ACTION_UPDATE)) { callbackData.setText("Got update: " + intent.getIntExtra("value", 0)); } else if (intent.getAction().equals(ACTION_STOPPED)) { callbackData.setText("STOPPED"); } } }; mLocalBroadcastManager.registerReceiver(mReceiver, filter); Button button = (Button) findViewById(R.id.start); button.setOnClickListener(mStartListener); button = (Button) findViewById(R.id.stop); button.setOnClickListener(mStopListener); } @Override protected void onDestroy() { super.onDestroy(); mLocalBroadcastManager.unregisterReceiver(mReceiver); } private OnClickListener mStartListener = new OnClickListener() { public void onClick(View v) { startService(new Intent(LocalServiceBroadcasterActivity.this, LocalService.class)); } }; private OnClickListener mStopListener = new OnClickListener() { public void onClick(View v) { stopService(new Intent(LocalServiceBroadcasterActivity.this, LocalService.class)); } }; public static class LocalService extends Service { LocalBroadcastManager mLocalBroadcastManager; int mCurUpdate; static final int MSG_UPDATE = 1; Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case MSG_UPDATE: { mCurUpdate++; Intent intent = new Intent(ACTION_UPDATE); intent.putExtra("value", mCurUpdate); mLocalBroadcastManager.sendBroadcast(intent); Message nmsg = mHandler.obtainMessage(MSG_UPDATE); mHandler.sendMessageDelayed(nmsg, 1000); } break; default: super.handleMessage(msg); } } }; @Override public void onCreate() { super.onCreate(); mLocalBroadcastManager = LocalBroadcastManager.getInstance(this); } public int onStartCommand(Intent intent, int flags, int startId) { // Tell any local interested parties about the start. mLocalBroadcastManager.sendBroadcast(new Intent(ACTION_STARTED)); // Prepare to do update reports. mHandler.removeMessages(MSG_UPDATE); Message msg = mHandler.obtainMessage(MSG_UPDATE); mHandler.sendMessageDelayed(msg, 1000); return ServiceCompat.START_STICKY; } @Override public void onDestroy() { super.onDestroy(); // Tell any local interested parties about the stop. mLocalBroadcastManager.sendBroadcast(new Intent(ACTION_STOPPED)); // Stop doing updates. mHandler.removeMessages(MSG_UPDATE); } @Override public IBinder onBind(Intent intent) { return null; } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。