定义:
后台运行,不可见,没有界面
优先级高于Activity
用途:
播放音乐、记录地理信息位置的改变、监听某种动作。。
注意:
运行在主线程,不能用它来做耗时的请求或者动作
可以在服务中开一个线程,在线程中做耗时动作
类型:
1、本地服务(Local Service)
应用程序内部
startService stopService stopSelf stopSelfResult
bindService unbindService
2、远程服务(Remote Service)
Android系统内部的应用程序之间
定义IBinder接口
Start方式特点:
服务跟启动源没有任何联系
无法得到服务对象
Bind方式特点:
通过Ibinder接口实例,返回一个ServiceConnection对象给启动源
通过ServiceConnection对象的相关方法可以得到Service对象
demo:
package com.example.mhy.myservice;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.example.mhy.myservice.MyBindService.MyBinder;
public class MainActivity extends AppCompatActivity {
Intent intent1;
Intent intent2;
private boolean unBindTag = false;
private boolean stopTag = false;
/**
* 服务是活动在主线程的,服务一定要注册
*/
private MyBindService service;
private ServiceConnection conn = new ServiceConnection() {
@Override //当服务跟启动源连接的时候 会自动回调
public void onServiceConnected(ComponentName name, IBinder binder) {
service = ((MyBinder)binder).getService();
}
@Override //当服务跟启动源断开的时候 会自动回调
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void doClick(View v) {
switch (v.getId()) {
case R.id.start://开启服务
intent1 = new Intent(MainActivity.this, MyStartService.class);
startService(intent1);
break;
case R.id.stop: //停止非绑定形式开启的服务
stopService(intent1);
stopTag = true;
break;
case R.id.bind: //绑定的形式启动服务,也可以用startService()启动后,
//再进行服务的绑定
//startService(intent2);
//bindService(intent2, conn, Service.BIND_AUTO_CREATE);
intent2 = new Intent(MainActivity.this, MyBindService.class);
bindService(intent2, conn, Service.BIND_AUTO_CREATE);//
break;
case R.id.next:
service.next();
break;
case R.id.pause:
service.pause();
break;
case R.id.pervious:
service.previous();
break;
case R.id.play:
service.play();
break;
case R.id.unbind:
unbindService(conn);
unBindTag = true;
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//当活动消亡后应停止服务
if (!stopTag) {
stopService(intent1);
}
//服务绑定后当活动消亡后应该解除服务的绑定
// if (!unBindTag) {
// unbindService(conn);
//
// }
}
}
package com.example.mhy.myservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
/**
* Created by mhy on 2016/5/7.
*/
public class MyStartService extends Service {
@Override
public void onCreate() {
Log.i("infor", "SerVice--onCreate()");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("infor", "Service--onStartCommand()");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.i("infor", "Service--onDestory");
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i("info", "Service--onBind()");
return null;
}
}
package com.example.mhy.myservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
/**
* Created by mhy on 2016/5/7.
*/
public class MyBindService extends Service {
/**
* Binder实现了IBinder,写一个Binder的子类增加一个getService()
* 得到当前service的实例,用于onBind()方法返回一个IBinder实例
* 父类出现的地方子类都可以替代,符合里氏代换原则
*/
public class MyBinder extends Binder {
public MyBindService getService() {
return MyBindService.this;
}
}
@Override
public boolean onUnbind(Intent intent) {
Log.i("infor", "MyBindService--onUnbind()");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
Log.i("infor", "MyBindService--onDestroy()");
super.onDestroy();
}
@Nullable
@Override //IBinder是一个接口需实现该接口
public IBinder onBind(Intent intent) {
Log.i("infor", "MyBindService--onBind()");
return new MyBinder();
}
@Override
public void onCreate() {
Log.i("infor", "MyBindService--onCreate()");
super.onCreate();
}
public void play() {
Log.i("infor", "MyBindService--play()");
}
public void pause() {
Log.i("infor", "MyBindService--pause()");
}
public void previous() {
Log.i("infor", "MyBindService--previous()");
}
public void next() {
Log.i("infor", "MyBindService--next()");
}
}