Android 广播介绍与service一起使用案例
1、案例
定义广播,注册广播,发送广播,最后注销广播
1.1、定义广播
private BroadcastReceiver scanReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) {if (intent.getAction().equals(BARCODE_ACTION)) { String data = intent.getStringExtra(BARCODE_ACTION_DATA); Log.e("MainActivity", "---------------------data-------------"+data); if(data!=null){ setText(data); manager.stopBarcodeScan(); } } } };
1.2、注册广播
//注册广播接收器 IntentFilter intentFilter=new IntentFilter(); intentFilter.addAction("com.example.rfid_sdk_as.receiver.action"); registerReceiver(scanReceiver,intentFilter);
1.3、发送广播
Intent intent = new Intent(BARCODE_ACTION); sendBroadcast(intent);
1.4、注销广播
if(scanReceiver!=null){ unregisterReceiver(scanReceiver); }
2、广播与service一起使用
2.1、定义广播
package com.example.rfid_sdk_as; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.RemoteException; import android.util.Log; import com.hyc.api.DeviceManagement; public class BarCodeReceiver extends BroadcastReceiver { public static final String RECEIVER_ACTION = "com.android.server.scannerservice.broadcastbl"; private DeviceManagement deviceManagement; private BarCodeReceiver receiver; public void setDeviceManagement(DeviceManagement deviceManagement){ this.deviceManagement = deviceManagement; } public void setReceiver(BarCodeReceiver receiver){ this.receiver = receiver; } @Override public void onReceive(final Context context, final Intent intent) { Log.e("MainActivity", "---------------------接收到广播-------------"); String action = intent.getAction(); if (RECEIVER_ACTION.equals(action)) { String data = intent.getStringExtra("scannerdata"); Log.e("MainActivity", "-------------------data=" + data); if(deviceManagement!=null){ BarCodeData.Stub barCodeData = new BarCodeData.Stub() { @Override public void GetBarCodeData(String msg, String data) throws RemoteException { Log.e("MainActivity", "扫描状态:" + msg + "\t扫描数据:" + data); intent.putExtra("scannerdata",data); context.sendBroadcast(intent); } }; deviceManagement.startBarcodeScan(barCodeData); } } } }
2.2、创建service
package com.example.rfid_sdk_as; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.os.IBinder; import android.os.Message; import android.os.RemoteException; import android.support.annotation.Nullable; import android.util.Log; import com.hyc.api.DeviceManagement; import com.hyc.bean.RFIDData; import com.hyc.bean.TagData; import java.util.List; public class APPService extends Service { private static final String TAG = "ServerService"; private DeviceManagement deviceManagement = null; BarCodeReceiver receiver = null; public static final String RECEIVER_ACTION = "com.example.rfid_sdk_as.receiver.action"; @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); deviceManagement = new DeviceManagement(this); receiver = new BarCodeReceiver(); receiver.setDeviceManagement(deviceManagement); receiver.setReceiver(receiver); registerReceiver(receiver,new IntentFilter(BarCodeReceiver.RECEIVER_ACTION)); Log.i(TAG,"--------------start service"); } @Override public void onDestroy() { super.onDestroy(); deviceManagement.exit(); if(receiver!=null){ unregisterReceiver(receiver); } Log.i(TAG,"--------------stop service"); } }
2.3、serviceManager
package com.example.rfidlibrary; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; import com.example.rfid_sdk_as.AIDLRFIDData; import com.example.rfid_sdk_as.AIDLTagData; import com.example.rfid_sdk_as.BarCodeData; import com.example.rfid_sdk_as.IAPPServiceRemoteBinder; import com.example.rfid_sdk_as.InitDeviceCallback; import java.util.List; public class RFIDReaderManager { private static final String TAG = "RFIDReaderManager"; private static final String PACKAGE = "com.example.rfid_sdk_as"; private static final String BIND_ACTION = "com.example.rfid_sdk_as.APPService"; private IAPPServiceRemoteBinder binderService; private static Context mContext; private static RFIDReaderManager mManager; private RFIDReaderServiceConnectListen mListener; public static RFIDReaderManager instance(Context context) { Log.i(TAG, "------------------instance-----------------"); mContext = context; if (mManager != null) { return mManager; } else { mManager = new RFIDReaderManager(); } return mManager; } public void serviceConnectListen(RFIDReaderServiceConnectListen listener) { Log.i(TAG, "---------------------ServiceConnect"); mListener = listener; Intent it = new Intent(); it.setAction(BIND_ACTION); it.setPackage(PACKAGE); // it.setComponent(new ComponentName(PACKAGE, BIND_ACTION)); mContext.bindService(it, mserviceConnection, mContext.BIND_AUTO_CREATE); } public void unbind(){ Log.i(TAG, "---------------------unbind"); if(mserviceConnection!=null){ mContext.unbindService(mserviceConnection); } binderService = null; mListener.disConnect(); } ServiceConnection mserviceConnection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { unbind(); } @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.i(TAG, "---------------------onServiceConnected"); binderService = IAPPServiceRemoteBinder.Stub.asInterface(service); mListener.connected(); } }; public interface RFIDReaderServiceConnectListen{ public void connected(); public void disConnect(); } }
2.4、service调用
RFIDReaderManager manager = RFIDReaderManager.instance(this); manager.serviceConnectListen(new RFIDReaderManager.RFIDReaderServiceConnectListen() { @Override public void disConnect() { // TODO Auto-generated method stub } @Override public void connected() { // TODO Auto-generated method stu } });
上面是监听到service,然后 调用service中的方法使用。