我有一壶酒 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>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了