Service,是android的四大组件之一,没有界面,优先级高于Activity,运行在后台,执行一些耗时的操作,比如播放音乐,执行数据的加载,请求。
注意的是:运行在主线程,不能用它来做耗时的请求或者动作可以在服务中开一个线程,在线程中做耗时动作
Service的启动与绑定:
工程概况:
布局只有四个按钮,不列:
清单文件的声明:
MainActivity:
package ivo_chuanzhi.service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity implements View.OnClickListener{
private Intent intent;
ServiceConnection conn = new ServiceConnection() {
//服务被绑定成功的时候回调
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("绑定成功");
}
//被绑定的服务断线时候会回调
@Override
public void onServiceDisconnected(ComponentName name) {
System.out.println("绑定失败");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//操作的是同一个Service,不管是一个Intent实例启动还是两个Intent实例
intent = new Intent(MainActivity.this, MyService.class);
Button btnStart = (Button) findViewById(R.id.btnStartService);
Button btnStop = (Button) findViewById(R.id.btnStopService);
Button btnBind = (Button) findViewById(R.id.btnBindService);
Button btnUnBind = (Button) findViewById(R.id.btnUbBindService);
btnStart.setOnClickListener(this);
btnStop.setOnClickListener(this);
btnBind.setOnClickListener(this);
btnUnBind.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnStartService:
//启动服务:1、startServicer 2、绑定服务
startService(intent);
break;
case R.id.btnStopService:
//停止服务
stopService(intent);
break;
case R.id.btnBindService:
//绑定服务
bindService(intent, conn, Context.BIND_AUTO_CREATE);
break;
case R.id.btnUbBindService:
//解除绑定
unbindService(conn);
break;
}
}
}
package ivo_chuanzhi.service;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class MyService extends Service {
private boolean ServicerRunning = true;
public MyService() {
}
//Service都必须实现的方法
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
//没绑定的话
//throw new UnsupportedOperationException("Not yet implemented");
//如果实现了绑定了,需要实现onBind接口,返回一个Binder对象,通过Binder可以实现与绑定的activity通讯
return new Binder();//可以重写Binder,方便业务与activity通讯
}
//在activity里面启动了startService(new Intent(MainActivity.this, MyService.class));
//的方法都会回调这个函数,可重复执行
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
/* new Thread(){
@Override
public void run() {
super.run();
while(true) {
System.out.println("服务在运行。。。");
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();*/
System.out.println();
return super.onStartCommand(intent, flags, startId);
}
//创建服务后第一个回调该函数,只执行一次
@Override
public void onCreate() {
super.onCreate();
System.out.println("我的服务被创建了");
new Thread(){
@Override
public void run() {
super.run();
while(ServicerRunning) {
System.out.println("服务在运行。。。");
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
//stopService被执行后会回调
@Override
public void onDestroy() {
//stopService和UnBind都会关掉服务
System.out.println("我的服务被关掉了");
ServicerRunning = false;
}
}
源码里面的注释已经很详细的,在此就不再赘述
谢谢