Android四大组件Service之bindService启动
16lz
2021-01-23
下面是绑定模式启动Service例程:
BindService代码:
package com.example.ibcondservice02;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;public class BindServiceText extends Service { //Service 处理函数//1.创建一个binder实例private MyBinder binder = new MyBinder(); // 创建一个binder类,类中能返回Service 对象(MyBinder)并创建一个实例 binderpublic class MyBinder extends Binder{ // 创建一个Binder实例public BindServiceText getService() // 返回当前服务对象{return BindServiceText.this;}}// 2.返回一个binder实例 , 传入到onServiceConnected 的IBinder参数中@Overridepublic IBinder onBind(Intent arg0) { //传入到onServiceConnected 的IBinder参数中// TODO Auto-generated method stubreturn binder; // 返回binder对象}public void MyMethod() // 创建一个公共的方法{System.out.println("BinderService!!!");}}
Activity代码:
package com.example.ibcondservice02;import com.example.ibcondservice02.BindServiceText.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 IboundService extends Activity {private Button StartButton;private Button StopButton;private Boolean isConnected=false; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_ibound_service);StartButton = (Button)findViewById(R.id.iboundStart);StopButton = (Button)findViewById(R.id.iboundStop);StartButton.setOnClickListener(listener);StopButton.setOnClickListener(listener);}OnClickListener listener = new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubswitch (arg0.getId()) {case R.id.iboundStart:bindService();break;case R.id.iboundStop:unbindService();break;default:break;}}};private void bindService() {// TODO Auto-generated method stubIntent intent2 = new Intent(IboundService.this,BindServiceText.class);bindService(intent2, conn, Context.BIND_AUTO_CREATE);}private void unbindService() {// TODO Auto-generated method stubif(isConnected){unbindService(conn);}}private ServiceConnection conn= new ServiceConnection() {// 建立绑定时启动,IBinder对象(binder)参数是在IBinder onBind();得到的对象,《------@Overridepublic void onServiceConnected(ComponentName arg0, IBinder binder) {// TODO Auto-generated method stubMyBinder mybinder=(MyBinder)binder; // 获取的是binderService类中onBind返回的binder对象// BindServiceText Service = mybinder.getService();Service.MyMethod();isConnected=true;}@Overridepublic void onServiceDisconnected(ComponentName arg0) {// TODO Auto-generated method stubisConnected=false;}};}
Androidmanifest.xml
更多相关文章
- android中AudioRecord采集音频的参数说明
- android animation的两种应用实例
- Android Activity切换动画详解及实例
- android实现开机自动播放音乐实例(Broadcast、Service)
- Android开发:观察者模式应用实例
- Android Jetpack应指南学习笔记8——Navigation(二)使用safe arg
- IntentService通过HandlerThread单独开启一个线程来处理所有Inte