我有一壶酒 Android学习之Service(1)--->BinderService方式
本文只讨论扩展Binder类
创建一个Binder.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" > <Button android:id="@+id/btnStartBinderService" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Start BinderService" /> <Button android:id="@+id/btnStopBinderService" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Stop BinderService" /> </LinearLayout>
创建一个BinderService.jvaa类,继承Service
package com.szy.service; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.util.Log; public class BinderService extends Service { private static final String TAG = "BinderService"; private MyBinder binder =new MyBinder(); public class MyBinder extends Binder { public BinderService getService() { return BinderService.this; } } @Override public IBinder onBind(Intent intent) { return binder; } public void MyMethod() { Log.i(TAG, "MyMethod()"); } }
再新建一个类BinderActivity.java继承Activity
package com.szy.service; import com.szy.service.BinderService.MyBinder; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class BinderActivity extends Activity { private Button btnStartBinderService; private Button btnStopBinderService; private Boolean isConnected = false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.binder); btnStartBinderService=(Button)findViewById(R.id.btnStartBinderService); btnStopBinderService=(Button)findViewById(R.id.btnStopBinderService); btnStartBinderService.setOnClickListener(listener); btnStopBinderService.setOnClickListener(listener); } private OnClickListener listener=new OnClickListener() { public void onClick(View v) { switch (v.getId()) { case R.id.btnStartBinderService: bindService(); break; case R.id.btnStopBinderService: unBind(); break; default: break; } } }; private void unBind() { if (isConnected) { unbindService(conn); } } private void bindService() { Intent intent=new Intent(BinderActivity.this, BinderService.class); bindService(intent, conn, Context.BIND_AUTO_CREATE); } private ServiceConnection conn=new ServiceConnection() { public void onServiceDisconnected(ComponentName name) { isConnected=false; } public void onServiceConnected(ComponentName name, IBinder binder) { MyBinder myBinder= (MyBinder)binder; BinderService service=myBinder.getService(); service.MyMethod(); isConnected=true; } }; }
修改下AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.szy.service" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> </activity> <activity android:name=".BinderActivity" android:label="@string/app_name"> </activity> <activity android:name=".IntentActivity" 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=".ExampleService" /> <service android:name=".BinderService" /> <service android:name=".MyService"/> <service android:name=".ExampleIntentService"/> </application> </manifest>