Android必背 - 001
Broadcast的使用:
1、写一个类继承Receiver,在回调函数onReceive()中执行收到广播后的动作代码;
private class MyReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Log.v(TAG, "onReceiver2"); Log.v(TAG, "Action = " + intent.getAction()); } }
2、清单文件中注册广播接收器
<!-- 注册广播接收器 --> <receiver android:name="com.android.broadcast1.MyReceiver"> <intent-filter > <action android:name="ACTION_TEST_BROADCAST"/> </intent-filter> </receiver>
除了在清单中可以注册Receiver,也可以在代码中注册和解注册:
Log.v(TAG, "register broadcast");
mReceiver = new MyReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION);
registerReceiver(mReceiver, filter);
Log.v(TAG, "unregister broadcast"); unregisterReceiver(mReceiver);
3、在需要的地方发出广播
Intent intent = new Intent(); intent.setAction(ACTION); sendBroadcast(intent);
Service的使用:
1、写一个类继承Service,onCreate()回调函数中开启一个线程完成服务任务;onBind()回调方法中安排与其他部件的数据交互;onDestroy()中停止线程;
单道
public void onCreate() { mBinder = new MyBinder(); mHandler = new Handler(); mHandler.postDelayed(mRunnable, 1000); super.onCreate(); }
方法postDelayed的作用是延迟多少毫秒后开始运行,而removeCallbacks方法是删除指定的Runnable对象,使线程对象停止运行。(参考:http://book.51cto.com/art/201211/363310.htm)
public void onDestroy() { Log.v(TAG, "onDestory"); mNum = 0; if(mHandler != null){ mHandler.removeCallbacks(mRunnable); } super.onDestroy(); }
public IBinder onBind(Intent intent) { //返回IBinder对象给绑定者(也就是MainActivity) return mBinder; }其中需要写出实例化一个Runable对象,以及写好Binder类。
private Runnable mRunnable = new Runnable(){ public void run() { Log.v(TAG, "mNum count = " + mNum); mHandler.postDelayed(mRunnable, 1000); } }; public class MyBinder extends Binder{ /** 返回MyService实例 */ public MyService getServiceInstance(){ return MyService.this; } }
Binder是Android系统进程间通信(IPC)方式之一。Binder基于 Client-Server通信模式,传输过程只需一次拷贝,为发送发添加UID/PID身份,既支持实名Binder也支持匿名Binder,安全性高。(参考:http://www.linuxidc.com/Linux/2011-07/39271.htm)
绑定服务时,需要传入一个参数,为ServiceConnection对象,重写该对象的onServiceConnected()方法时完成数据的接收工作。
2、清单文件注册服务
3、启动服务:两种方法
方法一:startService(),运行于主线程中,需要手动stopService()
mIntent = new Intent(MainActivity.this, MyService.class); //启动Service MainActivity.this.startService(mIntent); mStartFlag = true;方法二:bindService(),两种方法结束服务:(1)随调用者销毁;(2)主动unbindService()
mIntent = new Intent(MainActivity.this, MyService.class); //BIND_DEBUG_UNBIND用于调试 //BIND_AUTO_CREATE默认使用 //启动Service MainActivity.this.bindService(mIntent, mConnection, Context.BIND_AUTO_CREATE); mBindFlag = true;
//停止Service if(mStartFlag){Log.v(TAG, "stopService"); MainActivity.this.stopService(mIntent); mStartFlag = false;}if(mBindFlag){ MainActivity.this.unbindService(mConnection); mBindFlag = false; }
如果需要与服务需要数据交互,在bindService时需要传入ServiceConnection对象,在该类的回调方法中接收IBinder对象,完成数据交互。
private ServiceConnection mConnection = new ServiceConnection(){ @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub //这里的IBinder就是Service中onBind中的返回的IBinder MyBinder binder = (MyBinder)service; MyService myService = binder.getServiceInstance(); //得到MyService对象就可以调用其函数得到数据 myService.getNum(); Log.v(TAG, "onServiceConnected"); } @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub Log.v(TAG, "onServiceDisconnected"); } };
Service与其他部件交互除了使用IBinder外还可以使用Broadcast。
具体的做法是直接在Service的回调方法onCreate()中发送broadcast,再在activity中reciver获得数据。
//在Service中发送广播 Intent intent = new Intent(); intent.setAction(ACTION); sendBroadcast(intent);
自定义action和permition:http://www.2cto.com/kf/201104/87303.html
系统中自带的action列表总结:http://blog.csdn.net/zhen520/article/details/7209272

浙公网安备 33010602011771号