package com.example.test2;
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;
import com.example.test2.MyService.MyBinder;
/**
* @author Rain
* date: 2015年3月18日 下午11:10:42 <br/>
* Copyright (c) 2015, 珠海中慧微电子 All Rights Reserved.
*/
public class BindServiceTest extends Activity {
private Button btn_start = null;
private Button btn_stop = null;
private boolean flag = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bind_service_test);
btn_start = (Button)findViewById(R.id.btn_start);
btn_start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(BindServiceTest.this, MyService.class);
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}
});
btn_stop = (Button)findViewById(R.id.btn_stop);
btn_stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
System.out.println("==activity===准备解绑=========");
//unbindService(conn);//无需调用,因为当前activity结束时,service会自动结束
BindServiceTest.this.finish();
}
});
flag = false;
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
System.out.println("==activity=====onDestroy======");
super.onDestroy();
unbindService(conn);//若不在此调用解绑service的方法,会报xxxActivity has leaked ServiceConnection的异常
}
private ServiceConnection conn = new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
System.out.println("==activity=====onServiceConnected======");
MyBinder myBinder = (MyBinder)binder;
MyService myService = myBinder.getService();
myService.myMethod();
flag = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
System.out.println("===activity=====onServiceDisconnected======");
}
};
}
package com.example.test2;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
/**
* @author Rain
* date: 2015年3月18日 下午11:06:55 <br/>
* Copyright (c) 2015, 珠海中慧微电子 All Rights Reserved.
*/
public class MyService extends Service {
private MyBinder myBinder = new MyBinder();
private boolean isRun = true;
@Override
public IBinder onBind(Intent intent) {
System.out.println("====MyService========onBind=======");
return myBinder;
}
@Override
public boolean onUnbind(Intent intent) {
System.out.println("====MyService========onUnbind=======");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
System.out.println("====MyService========onDestroy=======");
isRun = false;
super.onDestroy();
}
public class MyBinder extends Binder {
public MyService getService(){
return MyService.this;
}
}
public void myMethod(){
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("=======线程开始========");
while(isRun){
System.out.println("=======线程正在运行========");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("=======线程结束========");
}
}).start();
}
}
<LinearLayout 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=".MainActivity" >
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 启动 "/>
<Button
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 停止 "/>
</LinearLayout>
最后记得在manifest.xml中添加
<service android:name="com.example.test2.MyService" />