Service服务
定义:
-后台运行,不可见,没有界面
-优先级高于Activity
用途:
-播放音乐、记录地理信息位置的改变、监听某种动作
注意:
-运行在主线程,不能用它来做耗时的请求或者动作
-可以在服务中开一个线程,在线程中做耗时动作
类型
1、本地服务(Local Service)
-应用程序内部
-startService stopService stopSelf stopSelfResult
-bindService unbindService
2、远程服务(Remote Service)
-Android系统内部的应用程序之间
-定义IBinder接口
启动服务
Strat方式特点:
-服务跟启动源没有任何联系
-无法得到服务对象
Bind方式特点:
-通过Ibinder接口实例,返回一个ServiceConnecion对象给启动源
-通过ServiceConnection对象的相关方法可以得到Service对象
绑定本地Service并与之通信
如果Service和访问者之间需要进行方法调用或交换数据,则应该使用bindService()和unbindService()方法启动、关闭Service。
小demo,绑定本地Service并与之通信
public class BindServiceActivity extends AppCompatActivity {
Button bind,unbind,getServiceStatus;
BindService.MyBinder binder;
private ServiceConnection conn = new ServiceConnection() {
@Override//当该Activity与Service连接成功时回调此方法
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("Service is connected");
binder = (BindService.MyBinder) service;
}
@Override//当该Activity与Service断开连接时回调此方法
public void onServiceDisconnected(ComponentName name) {
System.out.println("Service is disConnected");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bind_service);
bind = (Button) findViewById(R.id.bind);
unbind = (Button) findViewById(R.id.unbind);
getServiceStatus = (Button) findViewById(R.id.getServiceStatus);
final Intent intent = new Intent(this,BindService.class);
bind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//绑定Service
bindService(intent,conn, Service.BIND_AUTO_CREATE);
}
});
unbind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//解除绑定Service
unbindService(conn);
}
});
getServiceStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取Service状态
Toast.makeText(BindServiceActivity.this, "Service的count值为:"+binder.getCount(), Toast.LENGTH_SHORT).show();
}
});
}
}
服务
public class BindService extends Service{
private int count;
private boolean quit;
private MyBinder binder = new MyBinder();
public class MyBinder extends Binder{
public int getCount(){
//获取Service的运行状态:count
return count;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
System.out.println("Service is Binded");
return binder;
}
@Override//Service别创建时回调该方法
public void onCreate() {
super.onCreate();
System.out.println("Service is Created");
new Thread(){
@Override
public void run() {
while (!quit){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
}
}
}.start();
}
@Override//Service被断开连接时调用该方法
public boolean onUnbind(Intent intent) {
System.out.println("Service is Unbinded");
return true;
}
@Override//Service关闭时回调此方法
public void onDestroy() {
super.onDestroy();
this.quit = true;
System.out.println("Service is Destroy");
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/bind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="绑定服务" />
<Button
android:id="@+id/unbind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消绑定" />
<Button
android:id="@+id/getServiceStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="获取服务状态" />
</LinearLayout>
配置Service
<service android:name=".service.BindService" />