
案例演示
布局文件
-
<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"
-
tools:context="${packageName}.${activityClass}" >
-
-
<Button
-
android:id="@+id/btn_bind"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:layout_alignParentLeft="true"
-
android:layout_below="@+id/btn_stop"
-
android:onClick="click"
-
android:text="context.bindService" />
-
-
<Button
-
android:id="@+id/btn_unbind"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:layout_below="@+id/btn_bind"
-
android:onClick="click"
-
android:text="context.unbindService" />
-
-
-
</RelativeLayout>
注冊service
-
<?xml version="1.0" encoding="utf-8"?>
-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
-
package="com.example.android_servicedemo"
-
android:versionCode="1"
-
android:versionName="1.0" >
-
-
<uses-sdk
-
android:minSdkVersion="14"
-
android:targetSdkVersion="19" />
-
-
<application
-
android:allowBackup="true"
-
android:icon="@drawable/ic_launcher"
-
android:label="@string/app_name"
-
android:theme="@style/AppTheme" >
-
<activity
-
android:name="com.example.android_servicedemo.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="ServiceTese"
-
android:permission="com.example.permission.servers" >
-
<intent-filter>
-
<action android:name="com.example.serversdemo.action" />
-
</intent-filter>
-
</service>
-
</application>
-
-
</manifest>
主activity
package com.example.android_servicedemo;
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.util.Log;
import android.view.View;
public class MainActivity extends Activity {
private Intent service;
MyConn myConn;
public static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 隐士意图的启动
service = new Intent();
service.setAction("com.example.serversdemo.action");
}
public void click(View v) {
int id = v.getId();
switch (id) {
case R.id.btn_bind:
// con:该參数是一个ServiceConnection对象。该对象用于监听訪问者与service之间的连接情况
// flag:指定绑定时是否自己主动创建service(假设service还没创建)0:不自己主动创建。BIND_AUTO_CREATE自己主动创建
myConn = new MyConn();
this.bindService(service, myConn, Service.BIND_AUTO_CREATE);
break;
case R.id.btn_unbind:
if (myConn != null) {
this.unbindService(myConn);
}
break;
default:
break;
}
}
class MyConn implements ServiceConnection {
/**
* 当连接成功的时候回调此方法
*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
Log.i(TAG, "---------onServiceConnected" + name);
}
/**
* 当service所在的宿主矜持因为异常终止或因为其它原因终止,导致该service与訪问者之间断开连接的时候回调
*/
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, "---------onServiceDisconnected" + name);
}
}
}
执行结果
点击context.bindService--->点击context.unbindService。生命周期为
点击context.bindService--->home键--->点击context.bindService--->点击context.bindService..
.--->点击context.stopService

所以仅仅能点击一次unbind
startService生命周期见http://blog.csdn.net/zhaoyazhi2129/article/details/32711481