�����ڵ�λ�ã���ҳ > ���� > Android���� > ����
Androidϵͳ�еĹ㲥��Broadcast�����Ƽ�Ҫ���ܺ�ѧϰ�ƻ�
http://www.drovik.com/      2013-1-20 16:19:17      ��Դ�����޵�Android֮��      �����
 ��Androidϵͳ�У��㲥��Broadcast���������֮�䴫�����ݣ�Intent����һ�ֻ��ƣ���Щ��������ǿ���λ�ڲ�ͬ�Ľ����У�����������Binder����һ�����𵽽��̼�ͨ�ŵ����ã�����ͨ��һ���򵥵�������ѧϰAndroidϵͳ�Ĺ㲥���ƣ�Ϊ���������㲥���Ƶ�Դ������׼����

��Androidϵͳ�У�Ϊʲô��Ҫ�㲥�����أ��㲥���ƣ�������������һ��������ͨ�ŷ�ʽ��������������λ�ڲ�ͬ�Ľ��̵��У���ô������Binder������ʵ�֣���������������ͬһ�������У���ô����֮���������ͨ�ŵķ�ʽ�͸����ˣ������������㲥�����ƺ��Ƕ���ġ�Ȼ�����㲥����ȴ�Dz�������ģ�����Binder���Ʋ�һ���ĵط����ڣ��㲥�ķ����ߺͽ����������Dz���Ҫ֪���Է��Ĵ��ڵģ����������ĺô����ǣ�ϵͳ�ĸ��������������ϵ���֯��һ������ϵͳ�;��и߶ȵĿ���չ�ԣ�����������ϵͳ���м��ɡ�

�����������У��Ƿdz�ǿ��ģ��֮��ĸ��ھ۵�����Եģ���Ȼ�Ļ�������ϵͳԽ��Խ�Ӵ󣬾ͻ�������Խ��Խ��ά���ķ��գ������������Ŀ��ʧ�ܡ�AndroidӦ�ó������֯��ʽ������˵�ǰ����ָ��ھ۵�����Ե�˼��᳹�÷dz�͸�������κ�һ��Activity�У�������ʹ��һ���򵥵�Intent��ͨ��startActivity����startService���Ϳ��԰�����һ��Activity����Service��������Ϊ�����񣬶����������ϲ��������Activity����Service��ʵ�֣�ֻ��Ҫ֪�������ַ�����ʽ�����ּ��ɣ����㲥���Ƹ��������������ߵ����ֶ�����Ҫ֪����

��������˵�������㲥������Androidϵͳ�У�Ҳ������ʲô���µĶ�������������˽�J2EE����COM���ͻ�֪������J2EE�У��ṩ����Ϣ����Bean��Message-Driven Bean��������ʵ��Ӧ�ó���������֮�����Ϣ���ݣ�����COM�У��ṩ�����ӵ㣨Connection Point���ĸ��Ҳ��������Ӧ�ó���������������Ϣ���ݡ�������J2EE�е���Ϣ����Bean������COM�е����ӵ㣬����Androidϵͳ�Ĺ㲥���ƣ����ǵ�ʵ�ֻ���������Ϣ����/����ģʽ���¼�����ģ�ͣ���Ϣ�������߷����¼�����ʹ���߶��ĸ���Ȥ���¼���

�ϻ�˵��һ��ѣ����ڿ�ʼ���������ˣ���ǰ�������һ��������ͨ�����������������Androidϵͳ�Ĺ㲥���ơ�����������У���һ��Service����������һ���߳���ʵ����һ������������ÿ��һ���Ӿ��Զ���1��Ȼ�󽫽�����ϵط�����Ӧ�ó����еĽ����̣߳��������߳��е�Activity�ڵõ���������󣬾ͻ�ѽ����ʾ�ڽ����ϡ�ΪʲôҪ�Ѽ����������������һ���߳��н����أ����ǿ��԰���������������������һ����ʱ�ļ������߼���������ڽ����߳���ȥʵ�֣���ô�Ʊؾͻᵼ��Ӧ�ó�������Ӧ�����¼��������Ӧ�ó������ANR��Application Not Responding�����⡣�������߳�Ϊ�˰Ѽ�1�������ԴԴ���ϵط����������̣߳���ʱ��Ϳ��Կ���ʹ�ù㲥�����ˡ�

������AndroidԴ���빤���д���һ��AndroidӦ�ó��򹤳̣����־ͳ�ΪBroadcast�ɡ�������λ��AndroidԴ���빤�̣���ο���Ubuntu�����ء�����Ͱ�װAndroid����Դ����һ�ģ����������AndroidԴ���빤���д���Ӧ�ó��򹤳̣���ο���Ubuntu��ΪAndroidϵͳ����JavaӦ�ó������Application Frameworks���Ӳ������һ�ġ����Ӧ�ó��򹤳̶�����һ����Ϊshy.luo.broadcast��package��������ӵ�Դ������Ҫ����ʵ���������ˡ����棬������һ�������package������ļ���

���ȣ�������src/shy/luo/broadcast/ICounterService.java�ļ��ж���������ķ���ӿڣ�

  1. package shy.luo.broadcast;
  2. public interface ICounterService {
  3. public void startCounter(int initVal);
  4. public void stopCounter();
  5. }
package shy.luo.broadcast; public interface ICounterService { public void startCounter(int initVal); public void stopCounter(); }����ӿںܼ򵥣���ֻ��������Ա�������ֱ�����������ֹͣ����������������ʱ��������ָ���������ij�ʼֵ��

���ţ���������һ��Ӧ�ó����Ĭ��Activity��ʵ�֣���src/shy/luo/broadcast/MainActivity.java�ļ��У�

  1. package shy.luo.broadcast;
  2. import android.app.Activity;
  3. import android.content.BroadcastReceiver;
  4. import android.content.ComponentName;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.IntentFilter;
  8. import android.content.ServiceConnection;
  9. import android.os.Bundle;
  10. import android.os.IBinder;
  11. import android.util.Log;
  12. import android.view.View;
  13. import android.view.View.OnClickListener;
  14. import android.widget.Button;
  15. import android.widget.TextView;
  16. public class MainActivity extends Activity implements OnClickListener {
  17. private final static String LOG_TAG = "shy.luo.broadcast.MainActivity";
  18. private Button startButton = null;
  19. private Button stopButton = null;
  20. private TextView counterText = null;
  21. private ICounterService counterService = null;
  22. @Override
  23. public void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.main);
  26. startButton = (Button)findViewById(R.id.button_start);
  27. stopButton = (Button)findViewById(R.id.button_stop);
  28. counterText = (TextView)findViewById(R.id.textview_counter);
  29. startButton.setOnClickListener(this);
  30. stopButton.setOnClickListener(this);
  31. startButton.setEnabled(true);
  32. stopButton.setEnabled(false);
  33. Intent bindIntent = new Intent(MainActivity.this, CounterService.class);
  34. bindService(bindIntent, serviceConnection, Context.BIND_AUTO_CREATE);
  35. Log.i(LOG_TAG, "Main Activity Created.");
  36. }
  37. @Override
  38. public void onResume() {
  39. super.onResume();
  40. IntentFilter counterActionFilter = new IntentFilter(CounterService.BROADCAST_COUNTER_ACTION);
  41. registerReceiver(counterActionReceiver, counterActionFilter);
  42. }
  43. @Override
  44. public void onPause() {
  45. super.onPause();
  46. unregisterReceiver(counterActionReceiver);
  47. }
  48. @Override
  49. public void onDestroy() {
  50. super.onDestroy();
  51. unbindService(serviceConnection);
  52. }
  53. @Override
  54. public void onClick(View v) {
  55. if(v.equals(startButton)) {
  56. if(counterService != null) {
  57. counterService.startCounter(0);
  58. startButton.setEnabled(false);
  59. stopButton.setEnabled(true);
  60. }
  61. } else if(v.equals(stopButton)) {
  62. if(counterService != null) {
  63. counterService.stopCounter();
  64. startButton.setEnabled(true);
  65. stopButton.setEnabled(false);
  66. }
  67. }
  68. }
  69. private BroadcastReceiver counterActionReceiver = new BroadcastReceiver(){
  70. public void onReceive(Context context, Intent intent) {
  71. int counter = intent.getIntExtra(CounterService.COUNTER_VALUE, 0);
  72. String text = String.valueOf(counter);
  73. counterText.setText(text);
  74. Log.i(LOG_TAG, "Receive counter event");
  75. }
  76. };
  77. private ServiceConnection serviceConnection = new ServiceConnection() {
  78. public void onServiceConnected(ComponentName className, IBinder service) {
  79. counterService = ((CounterService.CounterBinder)service).getService();
  80. Log.i(LOG_TAG, "Counter Service Connected");
  81. }
  82. public void onServiceDisconnected(ComponentName className) {
  83. counterService = null;
  84. Log.i(LOG_TAG, "Counter Service Disconnected");
  85. }
  86. };
  87. }
package shy.luo.broadcast; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { private final static String LOG_TAG = "shy.luo.broadcast.MainActivity"; private Button startButton = null; private Button stopButton = null; private TextView counterText = null; private ICounterService counterService = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startButton = (Button)findViewById(R.id.button_start); stopButton = (Button)findViewById(R.id.button_stop); counterText = (TextView)findViewById(R.id.textview_counter); startButton.setOnClickListener(this); stopButton.setOnClickListener(this); startButton.setEnabled(true); stopButton.setEnabled(false); Intent bindIntent = new Intent(MainActivity.this, CounterService.class); bindService(bindIntent, serviceConnection, Context.BIND_AUTO_CREATE); Log.i(LOG_TAG, "Main Activity Created."); } @Override public void onResume() { super.onResume(); IntentFilter counterActionFilter = new IntentFilter(CounterService.BROADCAST_COUNTER_ACTION); registerReceiver(counterActionReceiver, counterActionFilter); } @Override public void onPause() { super.onPause(); unregisterReceiver(counterActionReceiver); } @Override public void onDestroy() { super.onDestroy(); unbindService(serviceConnection); } @Override public void onClick(View v) { if(v.equals(startButton)) { if(counterService != null) { counterService.startCounter(0); startButton.setEnabled(false); stopButton.setEnabled(true); } } else if(v.equals(stopButton)) { if(counterService != null) { counterService.stopCounter(); startButton.setEnabled(true); stopButton.setEnabled(false); } } } private BroadcastReceiver counterActionReceiver = new BroadcastReceiver(){ public void onReceive(Context context, Intent intent) { int counter = intent.getIntExtra(CounterService.COUNTER_VALUE, 0); String text = String.valueOf(counter); counterText.setText(text); Log.i(LOG_TAG, "Receive counter event"); } }; private ServiceConnection serviceConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { counterService = ((CounterService.CounterBinder)service).getService(); Log.i(LOG_TAG, "Counter Service Connected"); } public void onServiceDisconnected(ComponentName className) { counterService = null; Log.i(LOG_TAG, "Counter Service Disconnected"); } }; }

MainActivity��ʵ��Ҳ�ܼ򵥣����ڴ�����onCreate����ʱ�򣬻����bindService�������Ѽ���������CounterService���������������ĵڶ�������serviceConnection��һ��ServiceConnectionʵ������������������������ϵͳ��������ʵ����onServiceConnected������һ��Binder���󴫻�����ͨ���������Binder�����getService�������Ϳ��Ի�ü���������ӿڡ�������������������ӿڱ�����MainActivity��counterService��Ա�����С�ͬ���������ǵ���unbindServiceֹͣ����������ʱ��ϵͳ��������ʵ����onServiceDisconnected��������MainActivity�������������������ӶϿ��ˡ�

ע�⣬����ͨ������bindService����������Serviceʱ�����Service����������Activity��λ��ͬһ�������У�������������ǰ��һƪ����Androidϵͳ���½����������Զ��������̣�startService����ԭ���������������������µĽ������������񣬺���������дһƪ����������bindService��������Ĺ��̡�

��MainActivity��onResume�����У�ͨ������registerReceiver����ע����һ���㲥������counterActionReceiver������һ��BroadcastReceiverʵ��������ָ��������㲥������ֻ��CounterService.BROADCAST_COUNTER_ACTION���͵Ĺ㲥����Ȥ����CounterService����һ��CounterService.BROADCAST_COUNTER_ACTION���͵Ĺ㲥ʱ��ϵͳ�ͻ������㲥���͵�counterActionReceiverʵ����onReceiver������ȥ����onReceive�����У��Ӳ���intent��ȡ����������ǰ��ֵ����ʾ�ڽ����ϡ�

MainActivity��������������ť���ֱ���Start Counter��Stop Counter��ť�����ǰ�߿�ʼ�����������������ֹͣ������

����������CounterServiceʵ����src/shy/luo/broadcast/CounterService.java�ļ��У�

  1. package shy.luo.broadcast;
  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.os.AsyncTask;
  5. import android.os.Binder;
  6. import android.os.IBinder;
  7. import android.util.Log;
  8. public class CounterService extends Service implements ICounterService {
  9. private final static String LOG_TAG = "shy.luo.broadcast.CounterService";
  10. public final static String BROADCAST_COUNTER_ACTION = "shy.luo.broadcast.COUNTER_ACTION";
  11. public final static String COUNTER_VALUE = "shy.luo.broadcast.counter.value";
  12. private boolean stop = false;
  13. private final IBinder binder = new CounterBinder();
  14. public class CounterBinder extends Binder {
  15. public CounterService getService() {
  16. return CounterService.this;
  17. }
  18. }
  19. @Override
  20. public IBinder onBind(Intent intent) {
  21. return binder;
  22. }
  23. @Override
  24. public void onCreate() {
  25. super.onCreate();
  26. Log.i(LOG_TAG, "Counter Service Created.");
  27. }
  28. @Override
  29. public void onDestroy() {
  30. Log.i(LOG_TAG, "Counter Service Destroyed.");
  31. }
  32. public void startCounter(int initVal) {
  33. AsyncTask<Integer, Integer, Integer> task = new AsyncTask<Integer, Integer, Integer>() {
  34. @Override
  35. protected Integer doInBackground(Integer... vals) {
  36. Integer initCounter = vals[0];
  37. stop = false;
  38. while(!stop) {
  39. publishProgress(initCounter);
  40. try {
  41. Thread.sleep(1000);
  42. } catch (InterruptedException e) {
  43. e.printStackTrace();
  44. }
  45. initCounter++;
  46. }
  47. return initCounter;
  48. }
  49. @Override
  50. protected void onProgressUpdate(Integer... values) {
  51. super.onProgressUpdate(values);
  52. int counter = values[0];
  53. Intent intent = new Intent(BROADCAST_COUNTER_ACTION);
  54. intent.putExtra(COUNTER_VALUE, counter);
  55. sendBroadcast(intent);
  56. }
  57. @Override
  58. protected void onPostExecute(Integer val) {
  59. int counter = val;
  60. Intent intent = new Intent(BROADCAST_COUNTER_ACTION);
  61. intent.putExtra(COUNTER_VALUE, counter);
  62. sendBroadcast(intent);
  63. }
  64. };
  65. task.execute(0);
  66. }
  67. public void stopCounter() {
  68. stop = true;
  69. }
  70. }
package shy.luo.broadcast; import android.app.Service; import android.content.Intent; import android.os.AsyncTask; import android.os.Binder; import android.os.IBinder; import android.util.Log; public class CounterService extends Service implements ICounterService { private final static String LOG_TAG = "shy.luo.broadcast.CounterService"; public final static String BROADCAST_COUNTER_ACTION = "shy.luo.broadcast.COUNTER_ACTION"; public final static String COUNTER_VALUE = "shy.luo.broadcast.counter.value"; private boolean stop = false; private final IBinder binder = new CounterBinder(); public class CounterBinder extends Binder { public CounterService getService() { return CounterService.this; } } @Override public IBinder onBind(Intent intent) { return binder; } @Override public void onCreate() { super.onCreate(); Log.i(LOG_TAG, "Counter Service Created."); } @Override public void onDestroy() { Log.i(LOG_TAG, "Counter Service Destroyed."); } public void startCounter(int initVal) { AsyncTask<Integer, Integer, Integer> task = new AsyncTask<Integer, Integer, Integer>() { @Override protected Integer doInBackground(Integer... vals) { Integer initCounter = vals[0]; stop = false; while(!stop) { publishProgress(initCounter); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } initCounter++; } return initCounter; } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); int counter = values[0]; Intent intent = new Intent(BROADCAST_COUNTER_ACTION); intent.putExtra(COUNTER_VALUE, counter); sendBroadcast(intent); } @Override protected void onPostExecute(Integer val) { int counter = val; Intent intent = new Intent(BROADCAST_COUNTER_ACTION); intent.putExtra(COUNTER_VALUE, counter); sendBroadcast(intent); } }; task.execute(0); } public void stopCounter() { stop = true; } }

�������������ʵ����ICounterService�ӿڡ����������binderService��������ʱ��ϵͳ���������onBind�����������������һ��Binder�����ϵͳ����������˵������MainActivity����bindService����������������������ʱ��ϵͳ�����MainActivity��ServiceConnectionʵ��serviceConnection��onServiceConnected����֪ͨMainActivity����������Ѿ��������ˣ����һ�ͨ���������������һ��BinderԶ�̶������BinderԶ�̶��������Դ�������onBind�ķ���ֵ�ˡ�

����onBind���ص�Binder������һ���Զ����CounterBinderʵ������ʵ����һ��getService��Ա��������ϵͳ֪ͨMainActivity�������������Ѿ����������������ӳɹ��󣬲��ҽ����Binder���󴫸�MainActivityʱ��MainActivity�ͻ�����Binder����ǿ��ת��ΪCounterBinderʵ����Ȼ���������getService������÷���ӿڡ�������MainActivity��ͨ�����Binder�����CounterService���������ˡ�

��MainActivity���ü���������ӿڵ�startCounter����ʱ�����������񲢲���ֱ�ӽ������״̬������ͨ��ʹ���첽����AsyncTask���ں�̨�߳��н��м���������ΪʲôҪʹ���첽�������ں�̨�߳��н��м����أ�ǰ������˵�����������������һ����ʱ�ļ������߼������ܰ������ڽ����߳��н��У���Ϊ�����CounterService����ʱ����û�����µĽ���������������MainActivityһ����������Ӧ�ó���Ľ����߳��У���ˣ�������Ҫʹ���첽�������ں�̨�߳��н��м�����

�첽����AsyncTask�ľ����÷����Բο��ٷ��ĵ�http://developer.android.com/reference/android/os/AsyncTask.html�����Ĵ���÷��ǣ������ǵ����첽����ʵ����execute��task.execute������ʱ����ǰ�����߳̾ͷ����ˣ�ϵͳ����һ����̨�߳���ִ������첽����ʵ����doInBackground�������������������������ִ�к�ʱ����ĵط��ˣ�������뵽һ��ѭ���У�ÿ��1���ӾͰѼ�������1��Ȼ��������ߣ�Thread.sleep�����ѹ��������������������̡��ڼ���Ĺ����У�����ͨ������publishProgress������֪ͨ�����ߵ�ǰ����Ľ��ȣ����õ����������½��棬����publishProgress������Ч�����վ���ֱ�뵽����첽����ʵ����onProgressUpdate�����У�����Ϳ��԰��������ֵ�Թ㲥����ʽ��sendBroadcast�����ͳ�ȥ�ˣ�����Ľ���ֵ�Ͷ���Ϊ��ǰ��������ļ���ֵ��

��MainActivity���ü���������ӿڵ�stopCounter����ʱ������ߺ���doInBackgroundִֹͣ�м�����stop = true�������ǣ�����doInBackground���˳�����ѭ����Ȼ�����ռ�����������ˣ����صĽ�������뵽onPostExecute�����У��������ͬ��ͨ���㲥����ʽ��sendBroadcast���������������㲥��ȥ��

����������ͽ��ܵ������ˣ��������ǿ���Ӧ�ó���������ļ�AndroidManifest.xml��

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="shy.luo.broadcast"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <application android:icon="@drawable/icon" android:label="@string/app_name">
  7. <activity android:name=".MainActivity"
  8. android:label="@string/app_name">
  9. <intent-filter>
  10. <action android:name="android.intent.action.MAIN" />
  11. <category android:name="android.intent.category.LAUNCHER" />
  12. </intent-filter>
  13. </activity>
  14. <service android:name=".CounterService"
  15. android:enabled="true">
  16. </service>
  17. </application>
  18. </manifest>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="shy.luo.broadcast" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".CounterService" android:enabled="true"> </service> </application> </manifest> ��������ļ��ܼ򵥣�ֻ�Ǹ���ϵͳ������һ��Activity��һ��Service��

������MainActivity�Ľ����ļ�����������res/layout/main.xml�ļ��У�

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:gravity="center">
  7. <LinearLayout
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:layout_marginBottom="10px"
  11. android:orientation="horizontal"
  12. android:gravity="center">
  13. <TextView
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_marginRight="4px"
  17. android:gravity="center"
  18. android:text="@string/counter">
  19. </TextView>
  20. <TextView
  21. android:id="@+id/textview_counter"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:gravity="center"
  25. android:text="0">
  26. </TextView>
  27. </LinearLayout>
  28. <LinearLayout
  29. android:layout_width="fill_parent"
  30. android:layout_height="wrap_content"
  31. android:orientation="horizontal"
  32. android:gravity="center">
  33. <Button
  34. android:id="@+id/button_start"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:gravity="center"
  38. android:text="@string/start">
  39. </Button>
  40. <Button
  41. android:id="@+id/button_stop"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:gravity="center"
  45. android:text="@string/stop" >
  46. </Button>
  47. </LinearLayout>
  48. </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10px" android:orientation="horizontal" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="4px" android:gravity="center" android:text="@string/counter"> </TextView> <TextView android:id="@+id/textview_counter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="0"> </TextView> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <Button android:id="@+id/button_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="@string/start"> </Button> <Button android:id="@+id/button_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="@string/stop" > </Button> </LinearLayout> </LinearLayout>������������ļ�Ҳ�ܼ򵥣���һ��������ģ���������Ӧ�ó������������󣬾Ϳ��Կ������Ľ�ͼ�ˡ�

Ӧ�ó����õ����ַ�����Դ�ļ�λ��res/values/strings.xml�ļ��У�

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="app_name">Broadcast</string>
  4. <string name="counter">Counter: </string>
  5. <string name="start">Start Counter</string>
  6. <string name="stop">Stop Counter</string>
  7. </resources>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Broadcast</string> <string name="counter">Counter: </string> <string name="start">Start Counter</string> <string name="stop">Stop Counter</string> </resources>������ǻ�Ҫ�ڹ���Ŀ¼�·���һ������ű��ļ�Android.mk��

  1. LOCAL_PATH:= $(call my-dir)
  2. include $(CLEAR_VARS)
  3. LOCAL_MODULE_TAGS := optional
  4. LOCAL_SRC_FILES := $(call all-subdir-java-files)
  5. LOCAL_PACKAGE_NAME := Broadcast
  6. include $(BUILD_PACKAGE)
LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE_TAGS := optional LOCAL_SRC_FILES := $(call all-subdir-java-files) LOCAL_PACKAGE_NAME := Broadcast include $(BUILD_PACKAGE) ��������Ҫ�����ˡ��й���ε�������AndroidԴ���빤�̵�ģ�飬�Լ���δ��system.img����ο���ε�������AndroidԴ�����е�ģ��һ�ġ�
ִ������������б���ʹ����
  1. USER-NAME@MACHINE-NAME:~/Android$ mmm packages/experimental/Broadcast
  2. USER-NAME@MACHINE-NAME:~/Android$ make snod
USER-NAME@MACHINE-NAME:~/Android$ mmm packages/experimental/Broadcast USER-NAME@MACHINE-NAME:~/Android$ make snod ����������õ�Androidϵͳ�����ļ�system.img�Ͱ�������ǰ�洴����BroadcastӦ�ó����ˡ�
�ٽ���������������ģ�������������ǵ������ˡ����������AndroidԴ���빤��������ģ��������ο���Ubuntu�����ء�����Ͱ�װAndroid����Դ����һ�ġ�
ִ��������������ģ������
  1. USER-NAME@MACHINE-NAME:~/Android$ emulator
USER-NAME@MACHINE-NAME:~/Android$ emulator

ģ���������𣬾Ϳ���App Launcher���ҵ�BroadcastӦ�ó���ͼ�꣬���Ű�������������Ȼ���������ϵ�Start Counter��ť���Ϳ��԰Ѽ������������������ˣ�������������ͨ���㲥�Ѽ���ֵ������MainActivity�����ǣ����Ǿͻ���MainActivity���濴����������ֵ���ϵ������ˣ�

������ʹ�ù㲥�����Ӿͽ������ˡ��ع�һ�£�ʹ�ù㲥���������裺

1. �㲥�Ľ�������Ҫͨ������registerReceiver��������ϵͳ������ʲô���Ĺ㲥����Ȥ����ָ��IntentFilter��������ϵͳע��㲥����������ָ��BroadcastReceiver��

  1. IntentFilter counterActionFilter = new IntentFilter(CounterService.BROADCAST_COUNTER_ACTION);
  2. registerReceiver(counterActionReceiver, counterActionFilter);
IntentFilter counterActionFilter = new IntentFilter(CounterService.BROADCAST_COUNTER_ACTION); registerReceiver(counterActionReceiver, counterActionFilter);

���ָ������Ȥ�Ĺ㲥����CounterService.BROADCAST_COUNTER_ACTION�ˣ���ָ���Ĺ㲥����������counterActonReceiver������һ��BroadcastReceiver���͵�ʵ����

2. �㲥�ķ�����ͨ������sendBroadcast����������һ��ָ���Ĺ㲥�����ҿ���ָ���㲥����ز�����

  1. Intent intent = new Intent(BROADCAST_COUNTER_ACTION);
  2. intent.putExtra(COUNTER_VALUE, counter);
  3. sendBroadcast(intent)
Intent intent = new Intent(BROADCAST_COUNTER_ACTION); intent.putExtra(COUNTER_VALUE, counter); sendBroadcast(intent)

���ָ���Ĺ㲥ΪCounterService.BROADCAST_COUNTER_ACTION�����Ҹ����Ĵ�������ǰ�ļ�����ֵcounter��������sendBroadcast����֮������ע����CounterService.BROADCAST_COUNTER_ACTION�㲥�Ľ����߱�����յ�����㲥�ˡ�

�ڵ�1���У��㲥�Ľ����߰ѹ㲥������ע�ᵽActivityManagerService�У��ڵ�2���У��㲥�ķ�����ͬ���ǰѹ㲥���͵�ActivityManagerService�У���ActivityManagerServiceȥ����ע��������㲥�Ľ����ߣ�Ȼ��ѹ㲥�ַ������ǡ�

�ڵ�2���ķַ��Ĺ��̣���ʵ���ǰ�����㲥ת����һ����Ϣ��Ȼ����뵽���������ڵ��߳���Ϣ������ȥ�����Ϳ�������Ϣѭ���е��ý�������onReceive�����ˡ�������һ��Ҫ�dz�ע��ĵط��ǣ�����ActivityManagerService������㲥�Ž����������ڵ��߳���Ϣ���к󣬾ͷ����ˣ��������������Ϣʲôʱ��ᱻ��������ˣ��Թ㲥�Ĵ������첽�ģ�������sendBroadcastʱ�������������ȴ�����㲥���������ŷ��ء�

��������Step 1��Step 4����ע��㲥�������Ĺ��̣�����Step 2ͨ��LoadedApk.getReceiverDispatcher��LoadedApk�ڲ�������һ��IIntentReceiver�ӿڣ����Ҵ��ݸ�ActivityManagerService�����������Step 5��Step 11�Ƿ��͹㲥�Ĺ��̣���Step 8�У�ActivityManagerService��������õ���IIntentReceiverԶ�̽ӿڣ�����LoadedApk.performReceiver�ӿڣ�LoadedApk.performReceiver�ӿ�ͨ��ActivityThread.H�ӿڵ�post����������㲥��Ϣ���뵽ActivityThread����Ϣ������ȥ����������Ϣ��LoadedApk��Args.run�����д�����LoadedApk.Args.run�������ŵ���MainActivity.BroadcastReceiver��onReceive���������մ�������㲥��

���¿�ʼ��ʱ�������ᵽ����������ӵ�����Ŀ�ģ���Ϊ�˽�һ��ѧϰAndroidϵͳ�Ĺ㲥���ƣ���ˣ��ڽ���������ƪ�����У����ǽ���ϸ��������ע��㲥�������ͷ��͹㲥�Ĺ��̣�

1. AndroidӦ�ó���ע��㲥��������registerReceiver���Ĺ��̷�����

2. AndroidӦ�ó����͹㲥��sendBroadcast���Ĺ��̷�����

����ѧϰ������ƪ���º��ܹ������Androidϵͳ�㲥���Ƶ��˽⣬�����ע��

 

���ij��ԣ�http://blog.csdn.net/luoshengyang/article/details/6730748

 

��������
��������(0)
���� *
�������� *
��֤�� *ͼƬ�����壿������µõ���֤��