服务
服务(service)是Android中实现程序后台运行的解决方案,它非常适合去执行那些不需要和用户交互而且还要求长期运行的任务。服务的运行不依赖于任何用户界面,即使程序被切换到后台,后者用户打开了另一个应用程序,服务仍然能够保持正常运行。
bindService
bindService是绑定Service服务,执行service服务中逻辑流程。service通过Context.startService()开始,通过Context.stopService()方法停止。bindService可以绑定已经启动的服务,也可以绑定没有启动的服务,绑定过后服务机会被启动,多个客户端可以绑定同一个服务;
启动流程
context.bindService() ——> onCreate() ——> onBind() ——> Service running ——> onUnbind() ——> onDestroy() ——> Service stop
StartService
1、调用startService方法,会创建一个service对象,同时启动这个service;
2、如果再次调用启动service会直接服用前面创建的service对象;
3、无论启动多少次,只需要调用一次stopService()就会停止;
bindService和startService示例
MainActivity
public class MainActivity extends Activity {
Button startServiceButton;// 启动服务按钮
Button shutDownServiceButton;// 关闭服务按钮
Button startBindServiceButton;// 启动绑定服务按钮
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWidget();
regiestListener();
}
/** 获得组件 */
public void getWidget() {
startServiceButton = (Button) findViewById(R.id.startServerButton);
startBindServiceButton = (Button) findViewById(R.id.startBindServerButton);
shutDownServiceButton = (Button) findViewById(R.id.sutdownServerButton);
}
/** 为按钮添加监听 */
public void regiestListener() {
startServiceButton.setOnClickListener(startService);
shutDownServiceButton.setOnClickListener(shutdownService);
startBindServiceButton.setOnClickListener(startBinderService);
}
/** 启动服务的事件监听 */
public Button.OnClickListener startService = new Button.OnClickListener() {
public void onClick(View view) {
/** 单击按钮时启动服务 */
Intent intent = new Intent(MainActivity.this,
CountService.class);
startService(intent);
Log.v("MainStadyServics", "start Service");
}
};
/** 关闭服务 */
public Button.OnClickListener shutdownService = new Button.OnClickListener() {
public void onClick(View view) {
/** 单击按钮时启动服务 */
Intent intent = new Intent(MainActivity.this,
CountService.class);
/** 退出Activity是,停止服务 */
stopService(intent);
Log.v("MainStadyServics", "shutDown serveice");
}
};
/** 打开绑定服务的Activity */
public Button.OnClickListener startBinderService = new Button.OnClickListener() {
public void onClick(View view) {
/** 单击按钮时启动服务 */
Intent intent = new Intent(MainActivity.this, UseBrider.class);
startActivity(intent);
Log.v("MainStadyServics", "start Binder Service");
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
service
/** 计数的服务 */
public class CountService extends Service {
/** 创建参数 */
boolean threadDisable;
int count;
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate() {
super.onCreate();
/** 创建一个线程,每秒计数器加一,并在控制台进行Log输出 */
new Thread(new Runnable() {
public void run() {
while (!threadDisable) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
count++;
Log.v("CountService", "Count is" + count);
}
}
}).start();
}
public void onDestroy() {
super.onDestroy();
/** 服务停止时,终止计数进程 */
this.threadDisable = true;
}
public int getConunt() {
return count;
}
//此方法是为了可以在Acitity中获得服务的实例
class ServiceBinder extends Binder {
public CountService getService() {
return CountService.this;
}
}
}
bindService
/** 通过bindService和unBindSerivce的方式启动和结束服务 */
public class UseBrider extends Activity {
/** 参数设置 */
CountService countService;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new UseBriderFace(this));
Intent intent = new Intent(UseBrider.this, CountService.class);
/** 进入Activity开始服务 */
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}
private ServiceConnection conn = new ServiceConnection() {
/** 获取服务对象时的操作 */
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
countService = ((CountService.ServiceBinder) service).getService();
}
/** 无法获取到服务对象时的操作 */
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
countService = null;
}
};
protected void onDestroy() {
super.onDestroy();
this.unbindService(conn);
Log.v("MainStadyServics", "out");
}
}
区别
1.通过startservice开启的服务.一旦服务开启, 这个服务和开启他的调用者之间就没有任何的关系了.
调用者不可以访问 service里面的方法. 调用者如果被系统回收了或者调用了ondestroy方法, service还会继续存在
2.通过bindService开启的服务,服务开启之后,调用者和服务之间 还存在着联系 ,
一旦调用者挂掉了.service也会跟着挂掉 .