3 相比在系统内发送全局广播,它更高效。
它的用法跟BroadcaseReceiver一样
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; } } }