Service 学习第二篇,我们使用BindService()来启动Service.
第一步,同样需要继承Service ,实现它必须的方法。并把这个Service注册到AndroidManifest.xml中,做为一种隐式意图,然后等待Activity的启动。
这里IBinder onBind(Intent intent);会返回一个IBinder 对象。就是我们Acitvity 与Service 交互的对象。
BindService.java
package com.hkrt.action;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class BindService extends Service {
private int count;
private boolean quit;
private MyBinder binder = new MyBinder();
public class MyBinder extends Binder {
public int getCount() {
return count;
}
}
@Override
public IBinder onBind(Intent intent) {
System.out.println("service is Binded");
return binder;
}
@Override
public void onCreate() {
System.out.println("Service is Create");
super.onCreate();
new Thread(){
@Override
public void run(){
while(!quit){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
}
}
}.start();
}
@Override
public boolean onUnbind(Intent intent) {
System.out.println("Service is Unbind");
return true;
}
@Override
public void onDestroy() {
System.out.println("Service is Destroy");
super.onDestroy();
this.quit=true;
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hkrt.action"
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=".ServiceDemo2Activity"
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=".BindService">
<intent-filter >
<action android:name="com.hkrt.action.BIND_SERVICE"/>
</intent-filter>
</service>
</application>
</manifest>
第二步:我们使用三个按钮,分别绑定Service和解除绑定,和获取Service状态。
main.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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TableLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="绑定Service"
android:id="@+id/bind"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="解除绑定"
android:id="@+id/unBind"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="获取Service状态"
android:id="@+id/serviceCount"
/>
</TableRow>
</TableLayout>
</LinearLayout>
效果图:
第三步:关键性代码。
package com.hkrt.action;
import android.app.Activity;
import android.app.Service;
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.widget.Button;
import android.widget.Toast;
public class ServiceDemo2Activity extends Activity {
Button bind, unBind, serviceCount;
BindService.MyBinder binder;
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
binder = (BindService.MyBinder) service;
System.out.println("service Connected");
}
@Override
public void onServiceDisconnected(ComponentName name) {
System.out.println("service disConnectioned");
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bind = (Button) findViewById(R.id.bind);
unBind = (Button) findViewById(R.id.unBind);
serviceCount = (Button) findViewById(R.id.serviceCount);
final Intent intent = new Intent();
intent.setAction("com.hkrt.action.BIND_SERVICE");
bind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bindService(intent, conn, Service.BIND_AUTO_CREATE);
}
});
unBind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
unbindService(conn);
}
});
serviceCount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ServiceDemo2Activity.this,
"service的count值为" + binder.getCount(), 1).show();
}
});
}
}
第四步:Service启动流程图。
Activity 与 Service 交互的结果。
Service的生命周期