和Activity非常类似,只是没有用户界面。
创建和配置Service(和Activity一样一样的~~)
步骤:
1.定义一个继承Service的子类。
2.在AndroidManifest.xml文件中配置Service
生命周期
onBind() 返回一个IBinder对象,应用程序可以通过它来和Service通信
onCreate() 第一次被创建时回调
onDestroy() Service关闭之前回调
onStartCommand() 客户端调用startService 启动服务时回调
onUnbind 当Service与所有绑定的客户端断开连接上回调
通过startService和stopService可以开启和关闭服务
绑定本地Service,并与之通信
bindService(Intent service , ServiceConnection conn,int flags)
1.service 要启动的意图
2.conn 该对象用于监听访问者与Service间的联系情况,当二者连接成功的时候,回调onServiceConnected , 否则,回调onServiceDisconnected
3.flags 绑定时是否自动创建Service,0 (不自动创建) BIND_AUTO_CREATE 自动创建
Service和Activity之间通信的思路:
1.创建一个类,实现Binder。并把要通信的数据和业务逻辑写在这个类中。
2.在Service的子类中,创建1类的对象,取名为binder.
3.在onBind中,return binder
4.在Manifest清单文件中,创建一个
<action android:name="com.vincentliong.BIND"/>
5.在Activity类中,创建一个MyBinder对象。
同时,把binder通过onBind方法传入到Activity中。进而实现与Activity间的数据通信。
示例:
Service
public class CountService extends Service {
private boolean quit;
private int count;
private MyBinder binder = new MyBinder();
//创建一个Binder的子类,把需要执行的操作写入
public class MyBinder extends Binder
{
public int getCount()
{
return count;
}
}
public CountService() {
}
@Override
public void onCreate() {
super.onCreate();
Log.i("Demos","Create Service");
new Thread(new Runnable() {
public void run() {
while(!quit)
{
try
{
Thread.sleep(1000);
count++;
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("Demos","Start Service");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
this.quit = true;
Log.i("Demos","Destroy Service");
}
public IBinder onBind(Intent intent) {
Log.i("Demos","Bind Service");
return binder;
}
@Override
public boolean onUnbind(Intent intent) {
Log.i("Demos","Unbind Service");
return super.onUnbind(intent);
}
}
Activity类
private Button mBtnStart;
private Button mBtnBind;
private Button mBtnStop;
private Button mBtnUnbind;
private Button mBtnCount;
private CountService.MyBinder binder ;
private ServiceConnection conn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_service_demo);
mBtnStart = (Button) findViewById(R.id.btn_start_service);
mBtnBind = (Button) findViewById(R.id.btn_bind_service);
mBtnStop = (Button) findViewById(R.id.btn_stop_service);
mBtnUnbind = (Button) findViewById(R.id.btn_unbind_service);
mBtnCount = (Button) findViewById(R.id.btn_getcount_service);
conn = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
binder = (CountService.MyBinder) service;
Log.i("Demo","serviceConnected");
}
public void onServiceDisconnected(ComponentName name) {
Log.i("Demo","serviceDisconnected");
}
};
mBtnStart.setOnClickListener(this);
mBtnBind.setOnClickListener(this);
mBtnStop.setOnClickListener(this);
mBtnUnbind.setOnClickListener(this);
mBtnCount.setOnClickListener(this);
}
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.vincentliong.service.BIND_SERVICE");
switch(v.getId())
{
case R.id.btn_start_service:
startService(intent);
break;
case R.id.btn_stop_service:
stopService(intent);
break;
case R.id.btn_bind_service:
bindService(intent,conn,BIND_AUTO_CREATE);
break;
case R.id.btn_unbind_service:
unbindService(conn);
break;
case R.id.btn_getcount_service:
Toast.makeText(this,binder.getCount()+"",Toast.LENGTH_SHORT).show();
break;
}
}
生命周期:
两种情况
1.调用startService()
onCreate –> onStartCommand –> Service运行中–>onDestroy
2.调用bindService()
onCreate –> onBind –>绑定Service –> 调用unbindService解绑 –> onUnbind –> onDestroy
注:解绑服务并不会关闭服务。
“特殊服务”^_^#~~~IntentService
使用队列来管理请求的Intent,当有Intent来请求启动IntentService的时候,IntentService会将该intent放到队列中,然后开启一个Worker线程来处理该Intent。为onBind onStartCommand提供了默认实现、
使用方法:
写一个类继承自IntentService
重写onHandleIntent
可以直接通过使用IntentService来完成耗时操作
版权声明:刚出锅的原创内容,希望对你有帮助~
原文地址:http://blog.csdn.net/liangyu2014/article/details/47709265