好记性不如铅笔头

android, 编程

Android应用开发笔记:broadcast

android使用broadcast和receiver来实现类之间的松耦合,降低了软件开发的成本。这里简单的备忘下自己写的一个receiver的模板。

CONTENTS

1)动态注册

public class LocationBroadcast 
{
	private static final String ACTION_STR = "LocationReceiver";
	private static final String SPEEDINFO_STR = "Locations.SpeedInfo";
	private static final String LOCDESC_STR = "Locations.LocDesc";
	
	private static class LocationReceiver extends BroadcastReceiver
	{
		private WeakReference<onLocationChangedListener> weak = null;
		private LocationReceiver(onLocationChangedListener listener) {
			super();
			this.weak = new WeakReference<LocationBroadcast.onLocationChangedListener>(listener);
		}
		@Override
		public void onReceive(Context context, Intent intent) 
		{
			onLocationChangedListener listener = weak.get();
			if (null == listener)
			{
				return;
			}
			listener.onLocationChanged((Locations.LocDesc)intent.getSerializableExtra(LOCDESC_STR),(Locations.SpeedInfo)intent.getSerializableExtra(SPEEDINFO_STR));
		}
	}
	
	public static interface onLocationChangedListener
	{
		public void onLocationChanged(Locations.LocDesc locDesc, Locations.SpeedInfo speedInfo);
	}
	
	public static void sendBroadcast(Context context,
			Locations.LocDesc locDesc, Locations.SpeedInfo speedInfo) 
	{
		if (null == context || null == locDesc || null == speedInfo) {
			return;
		}
		Intent intent = new Intent();
		intent.setAction(ACTION_STR);
		intent.putExtra(LOCDESC_STR, locDesc);
		intent.putExtra(SPEEDINFO_STR, speedInfo);
		context.sendBroadcast(intent);
	}

public static <T extends Context & onLocationChangedListener> BroadcastReceiver registerReceiver(T context) 
 {
 if (null == context)
 {
 return null;
 }
 LocationReceiver receiver = new LocationReceiver(context);
 IntentFilter filter = new IntentFilter();
 filter.addAction(ACTION_STR);
 context.registerReceiver(receiver, filter);
 return receiver;
 }
 public static <T extends Context & onLocationChangedListener> void unRegisterReceiver(T context, BroadcastReceiver rev) 
 {
 if (null == context || null == rev)
 {
 return;
 }
 try {
 context.unregisterReceiver(rev);
 } catch (Exception e) 
 {
 e.printStackTrace();
 }
 }

}

 使用方式:

public class SpeedActivity extends Activity implements LocationBroadcast.onLocationChangedListener 
{
private BroadcastReceiver receiver;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	        receiver = LocationBroadcast.registerReceiver(this);
}
@Override
 public void onLocationChanged(LocDesc locDesc, SpeedInfo speedInfo)
 { 
//....
}
@Override
	protected void onDestroy() {
	    LocationBroadcast.unRegisterReceiver(this, receiver);
		super.onDestroy();
	}
}

 注意:

1)调用register和unregister的context需要是同一个context,此处并没有做判断。如果后续有多个context都需要注册同一个receiver,那么可以在里面维护一个hashmap,根据不同的context返回不同的receiver的实例。这样就可以保证正确的ungister了。

2)静态注册

静态注册我没有现成代码可以备忘。暂时从网上找到一点,后续需要补齐。

<receiver android:name="com.xiaomo.view.broadcast.StaticReceiverTool">  
    <intent-filter>          
 <action android:name="com.xiaomo.view.broadcast01"></action>     
 </intent-filter>
</receiver>

 

备注:

1)broadcast有很多形式,比如有优先级的broadcast,权限的设置等,后续需要补齐。

2)资料【 http://www.cnblogs.com/jico/articles/1838293.html

           【 http://www.cnblogs.com/merryjd/archive/2013/01/06/2847312.html 】

            【 http://android.blog.51cto.com/268543/521784 】

          【 http://blog.csdn.net/hellogv/article/details/5999170 】

发表评论

5 × 1 =

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据