ANDROID_MARS学习笔记_S01原始版_017_绑定SERVICE

时间:2023-03-08 16:34:33

一、流程

1.编写service,重写onBind(Intent intent),返回自定义的Binder

2.自写义Binder,提供一个可访问的方法,以传递数据

3.点击界面按钮会开启service,过程为,通过Intent的setClass指定要开启的service,再通过bindService(intent, conn, BIND_AUTO_CREATE);开启service,其中参数conn是ServiceConnection,需要实现它的onServiceConnected(ComponentName name, IBinder service),service开启成功后,会回调这个函数,会把binder传给参数IBinder,从而实现数据传递,这就是所谓的给service提供了client-server模式

二、代码
1.xml
(1)activity_main.xml

 <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.service2.MainActivity" > <Button
android:id="@+id/bindBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="完成绑定操作" /> </RelativeLayout>

(2)AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.service2"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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=".SecondService"></service>
</application> </manifest>

2.java
(1)MainActivity.java

 package com.service2;

 import android.app.Activity;
import android.content.ComponentName;
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; import com.service2.SecondService.FirstBinder; public class MainActivity extends Activity { private Button bindBtn = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindBtn = (Button)findViewById(R.id.bindBtn);
bindBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondService.class);
bindService(intent, conn, BIND_AUTO_CREATE);
}
});
} ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
System.out.println("onServiceDisconnected>>>>>>");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
FirstBinder binder = (FirstBinder)service;
System.out.println("onServiceConnected>>>>>"+binder.getData());
}
};
}

(2)SecondService.java

 package com.service2;

 import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder; public class SecondService extends Service { @Override
public IBinder onBind(Intent intent) {
IBinder binder = new FirstBinder();
return binder;
} class FirstBinder extends Binder {
public String getData() {
return "test data";
}
}
}