class="article_content clearfix">
class="htmledit_views">
打印效果
如果Service和访问者之间需要进行方法调用或数据交换,则需要使用bindService()和unbindService()方法启动,关闭服务
BindService和Started Service都是Service,有什么地方不一样呢:
1. Started Service中使用StartService()方法来进行方法的调用,调用者和服务之间没有联系,即使调用者退出了,服务依然在进行【onCreate()- >onStartCommand()->startService()->onDestroy()】,注意其中没有onStart(),主要是被onStartCommand()方法给取代了,onStart方法不推荐使用了。
2. BindService中使用bindService()方法来绑定服务,调用者和绑定者绑在一起,调用者一旦退出服务也就终止了【onCreate()->onBind()->onUnbind()->onDestroy()】。
定义服务类
class="language-java">package WangLi.Service.BindService;
import class="tags" href="/tags/ANDROID.html" title=android>android.app.Service;
import class="tags" href="/tags/ANDROID.html" title=android>android.content.Intent;
import class="tags" href="/tags/ANDROID.html" title=android>android.os.Binder;
import class="tags" href="/tags/ANDROID.html" title=android>android.os.IBinder;
public class BindService extends Service {
private int count;
private boolean quit;
// 定义onBinder方法所返回的对象
private MyBinder binder = new MyBinder();
// 通过继承Binder来实现IBinder类
public class MyBinder extends Binder {
public int getCount()
{
//获取Service的运行状态:count
return count;
}
}
//必须实现的方法
@Override
public IBinder onBind(Intent arg0) {
System.out.println("Service is Binded");
//返回IBinder对象
return binder;
}
//Service被创建时回调该方法
@Override
public void onCreate()
{
super.onCreate();
System.out.println("Service is Created");
//启动一条线程,动态地修改count状态值
new Thread()
{
@Override
public void run()
{
while(!quit)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
}
count++;
}
}
}.start();
}
//Service断开连接时回调该方法
@Override
public boolean onUnbind(Intent intent)
{
System.out.println("Service is Unbinded");
return true;
}
//Serice被关闭之前回调
@Override
public void onDestroy()
{
super.onDestroy();
this.quit = true;
System.out.println("Service is Destroyed");
}
}
绑定,解除服务
class="language-java">package WangLi.Service.BindService;
import class="tags" href="/tags/ANDROID.html" title=android>android.app.Activity;
import class="tags" href="/tags/ANDROID.html" title=android>android.app.Service;
import class="tags" href="/tags/ANDROID.html" title=android>android.content.ComponentName;
import class="tags" href="/tags/ANDROID.html" title=android>android.content.Intent;
import class="tags" href="/tags/ANDROID.html" title=android>android.content.ServiceConnection;
import class="tags" href="/tags/ANDROID.html" title=android>android.os.Bundle;
import class="tags" href="/tags/ANDROID.html" title=android>android.os.IBinder;
import class="tags" href="/tags/ANDROID.html" title=android>android.view.View;
import class="tags" href="/tags/ANDROID.html" title=android>android.view.View.OnClickListener;
import class="tags" href="/tags/ANDROID.html" title=android>android.widget.Button;
import class="tags" href="/tags/ANDROID.html" title=android>android.widget.Toast;
public class MainActivity extends Activity {
Button bind, unbind, getServiceStatus;
// 保持所启动的Service的IBinder对象
BindService.MyBinder binder;
// 定义一个ServiceConnection对象
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder class="tags" href="/tags/SERVICE.html" title=service>service) {
System.out.println("--Service Connected--");
// 获取Service的onBind方法所返回的MyBinder对象
binder = (BindService.MyBinder) class="tags" href="/tags/SERVICE.html" title=service>service;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
System.out.println("--Service Disconnected--");
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取程序界面中的start,stop,getServiceStatus按钮
bind = (Button) findViewById(R.id.bind);
unbind = (Button) findViewById(R.id.unbind);
getServiceStatus = (Button) findViewById(R.id.getServiceStatus);
// 创建启动Service的Intent
final Intent intent = new Intent();
// 为Intent设置Action属性
intent.setAction("WangLi.Service.Bind_Service");
bind.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// 绑定指定的Service
bindService(intent, conn, Service.BIND_AUTO_CREATE);
}
});
unbind.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 解除绑定Service
unbindService(conn);
}
});
getServiceStatus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 获取并显示Service的count值
Toast.makeText(MainActivity.this,
"Service的count值为:" + binder.getCount(), 4000).show();
}
});
}
}
打印效果