��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�ļ��ж���������ķ���ӿڣ�
- package shy.luo.broadcast;
- public interface ICounterService {
- public void startCounter(int initVal);
- public void stopCounter();
- }
���ţ���������һ��Ӧ�ó����Ĭ��Activity��ʵ�֣���src/shy/luo/broadcast/MainActivity.java�ļ��У�
- 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�ļ��У�
- 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��
- <?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>
������MainActivity�Ľ����ļ�����������res/layout/main.xml�ļ��У�
- <?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�ļ��У�
- <?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>
- 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)
ִ������������б���ʹ����
- USER-NAME@MACHINE-NAME:~/Android$ mmm packages/experimental/Broadcast
- USER-NAME@MACHINE-NAME:~/Android$ make snod
�ٽ���������������ģ�������������ǵ������ˡ����������AndroidԴ���빤��������ģ��������ο���Ubuntu�����ء�����Ͱ�װAndroid����Դ����һ�ġ�
ִ��������������ģ������
- USER-NAME@MACHINE-NAME:~/Android$ emulator
ģ���������𣬾Ϳ���App Launcher���ҵ�BroadcastӦ�ó���ͼ�꣬���Ű�������������Ȼ���������ϵ�Start Counter��ť���Ϳ��Ѽ������������������ˣ�������������ͨ���㲥�Ѽ���ֵ������MainActivity�����ǣ����Ǿͻ���MainActivity���濴����������ֵ���ϵ������ˣ�
������ʹ�ù㲥�����Ӿͽ������ˡ��ع�һ�£�ʹ�ù㲥���������裺
1. �㲥�Ľ�������Ҫͨ������registerReceiver��������ϵͳ������ʲô���Ĺ㲥����Ȥ����ָ��IntentFilter��������ϵͳע��㲥����������ָ��BroadcastReceiver��
- IntentFilter counterActionFilter = new IntentFilter(CounterService.BROADCAST_COUNTER_ACTION);
- registerReceiver(counterActionReceiver, counterActionFilter);
���ָ������Ȥ�Ĺ㲥����CounterService.BROADCAST_COUNTER_ACTION�ˣ���ָ���Ĺ㲥����������counterActonReceiver������һ��BroadcastReceiver���͵�ʵ����
2. �㲥�ķ�����ͨ������sendBroadcast����������һ��ָ���Ĺ㲥�����ҿ���ָ���㲥����ز�����
- 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
- �������
- Android����������ndk-stack���ndk�����ж�̬��.so���������� (43�����)
- Android��������θı�RadioButton����ͼƬ�����ֵ����λ�� (179�����)
- android��������֮ADT��װ��ж�أ����� (13�����)
- Android����֮ListViewҳüҳ��Ч��VS android�������� (32�����)