Android启动服务和绑定服务的方法调用
2017-06-01 23:26
537 查看
我们通过一个demo来学习启动服务和绑定服务的方法调用,以以下情况来观察Service中的方法调用:
首次启动Service
多次启动Service
首次绑定Service
多次绑定Service
启动Service后再绑定Service
绑定Service后再启动Service
贴上代码,布局文件,MainActivity,MyService
首次启动Service打印如下:
多次启动Service
首次绑定Service
多次绑定Service
启动Service后再绑定Service
绑定Service后再启动Service
到这里这几种情况就分析完了,有兴趣的同学可以试下哈,需要注意的是:
1.service要在AndroidManifest.xml文件中声明。
2.同时启动并且绑定的服务要执行停止服务和解绑服务的调用才会打印onDestroy方法。
首次启动Service
多次启动Service
首次绑定Service
多次绑定Service
启动Service后再绑定Service
绑定Service后再启动Service
贴上代码,布局文件,MainActivity,MyService
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/start_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="start service" android:textAllCaps="false" /> <Button android:id="@+id/stop_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="stop service" android:textAllCaps="false" /> <Button android:id="@+id/bind_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="bind service" android:textAllCaps="false" /> <Button android:id="@+id/unbind_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="unbind service" android:textAllCaps="false" /> </LinearLayout>
public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.start_btn).setOnClickListener(this); findViewById(R.id.stop_btn).setOnClickListener(this); findViewById(R.id.bind_btn).setOnClickListener(this); findViewById(R.id.unbind_btn).setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.start_btn: startService(); break; case R.id.stop_btn: stopService(); break; case R.id.bind_btn: bindService(); break; case R.id.unbind_btn: unbindService(); break; default: break; } } /** * 启动服务 */ private void startService() { startService(new Intent(this, MyService.class)); } /** * 停止服务 */ private void stopService() { stopService(new Intent(this, MyService.class)); } private ServiceConnection mServiceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { } @Override public void onServiceDisconnected(ComponentName name) { } }; /** * 绑定服务 */ private void bindService() { bindService(new Intent(this, MyService.cl 4000 ass), mServiceConnection, BIND_AUTO_CREATE); } /** * 解绑服务 */ private void unbindService() { unbindService(mServiceConnection); } }
public class MyService extends Service { private static final String TAG = "MyService"; public MyService() { Log.e(TAG, "MyService()"); } @Override public void onCreate() { super.onCreate(); Log.e(TAG, "MyService -> onCreate"); } @Override public int onStartCommand(Intent intent,int flags, int startId) { Log.e(TAG, "MyService -> onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public IBinder onBind(Intent intent) { Log.e(TAG, "MyService -> onBind"); return new MyBind(); } @Override public void onDestroy() { super.onDestroy(); Log.e(TAG, "MyService -> onDestroy"); } private static class MyBind extends Binder{ } }
首次启动Service打印如下:
多次启动Service
首次绑定Service
多次绑定Service
启动Service后再绑定Service
绑定Service后再启动Service
到这里这几种情况就分析完了,有兴趣的同学可以试下哈,需要注意的是:
1.service要在AndroidManifest.xml文件中声明。
2.同时启动并且绑定的服务要执行停止服务和解绑服务的调用才会打印onDestroy方法。
相关文章推荐
- [android] 绑定方式开启服务&调用服务的方法
- android 学习笔记9-服务 启动停止 调用服务方法 远程服务 AIDL进程通信
- Android--绑定服务调用服务的方法
- Android -- service的开启方式, start开启和绑定开启服务,调用服务的的方法, aidl调用远程服务
- Android(java)学习笔记229:服务(service)之绑定服务调用服务里面的方法 (采用接口隐藏代码内部实现)
- Android(java)学习笔记228:服务(service)之绑定服务调用服务里面的方法
- Android--绑定服务调用服务的方法
- Androidx学习笔记(65)--- 通过绑定服务调用服务里的方法
- 安卓Service绑定方式启动服务并调用服务中的方法
- Android - 绑定服务调用服务里面的方法,start/bind开启服务的比较,混合方式开启服务,本地服务,远程服务,系统服务,录音API
- Android中服务里面方法的调用,开启,绑定【入门】
- Android绑定方式开始服务&调用服务当中的方法
- android之绑定服务调用服务的方法
- android,aidl,绑定远程服务,调用远程服务的方法
- Android-本地启动服务的方法及调用方法的顺序
- android90 bind方式启动服务service调用service里的方法
- 调用android非unbind的服务中的方法(不使用bindService启动的服务)
- 【黑马Android】(10)绑定的方式调用服务的方法/图片的各种操作/人脸识别
- Android -- service的开启方式, start开启和绑定开启服务,调用服务的的方法, aidl调用远程服务
- 91_采用绑定服务的方式调用服务里面的方法