在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"
android:background="#000000"
android:gravity="center_horizontal">
<Button
android:id="@+id/bind"
android:layout_marginTop="8dp"
android:layout_width="160dp"
android:layout_height="40dp"
android:background="#3399ff"
android:textColor="#ffffff"
android:text="绑定Service" />
<Button
android:id="@+id/unbind"
android:layout_marginTop="8dp"
android:layout_width="160dp"
android:layout_height="40dp"
android:background="#3399ff"
android:textColor="#ffffff"
android:text="解除Service绑定" />
</LinearLayout>
在MyServiceUtil.java中:
package com.li.servicedemo;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class MyServiceUtil extends Service {
private IBinder myBinder = new BinderImpl() ;
@Override
public IBinder onBind(Intent intent) {
System.out.println("*** Service onBind()") ;
return this.myBinder ; // 此处暂时不做任何的处理
}
class BinderImpl extends Binder implements IService{
@Override
public String getInterfaceDescriptor() {
return "MyServiceUitl Class .." ;
}
}
}
在Iservice.java中:
package com.li.servicedemo;
public interface IService {
}
在MyServiceDemo.java中:
package com.li.servicedemo;
import org.lxh.demo.R;
import com.li.servicedemo.MyServiceUtil.BinderImpl;
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 MyServiceDemo extends Activity {
private Button bind;
private Button unbind;
private IService service = null ;
private ServiceConnection serviceConnection = new ServiceConnectionImpl() ;
private class ServiceConnectionImpl implements ServiceConnection {
public void onServiceConnected(ComponentName name, IBinder service) {
MyServiceDemo.this.service = (BinderImpl) service ;
// BinderImpl是IBinder接口的子类,所以将IBinder向下转型为BinderImpl类
// 而后通过此类对象为Service接口实例化
}
public void onServiceDisconnected(ComponentName name) {
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.bind = (Button) super.findViewById(R.id.bind);
this.unbind = (Button) super.findViewById(R.id.unbind);
this.bind.setOnClickListener(new BindOnClickListenerImpl());
this.unbind.setOnClickListener(new UnbindOnClickListenerImpl());
}
private class BindOnClickListenerImpl implements OnClickListener {
public void onClick(View v) {
MyServiceDemo.this.bindService(new Intent(MyServiceDemo.this,
MyServiceUtil.class), MyServiceDemo.this.serviceConnection,
Context.BIND_AUTO_CREATE);
}
}
private class UnbindOnClickListenerImpl implements OnClickListener {
public void onClick(View v) {
if(MyServiceDemo.this.service != null) {
MyServiceDemo.this
.unbindService(MyServiceDemo.this.serviceConnection);
MyServiceDemo.this.service = null ;
}
}
}
}
修改AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.lxh.demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.li.servicedemo.MyServiceDemo"
android:label="@string/title_activity_my_service_demo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.li.servicedemo.MyServiceUtil" />
</application>
</manifest>