1.Service概述
定义: 1)后台运行,不可见,没有界面
2)优先级高于Activity
3)一般不可以kill
用途: 播放音乐,记录地理信息位置的改变,监听某种动作....
注意: 运行在主线程,不能用它来做耗时的请求或动作
可以在服务中开一个线程,在线程中做耗时动作
类型:本地服务(Local Service)
在应用程序内部,有两种方式:
1)startService (开始) stopService stopSelf stopSelfResult(结束)
2)bindService unbindService--绑定服务
远程服务(Remote Service)
android系统内部的应用程序之间
Start方式的特点:服务跟启动源没有任何联系,无法得到服务对象
Bind方式的特点:1)通过Ibinder接口实例,返回一个ServiceConnection对象给启动源
2)通过ServiceConnection对象的相关方法可以得到Service对象
2.
service_layout.xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start:"
android:id="@+id/tv1"
/>
<Button
android:layout_width="match_parent"
android:onClick="doClick"
android:layout_height="wrap_content"
android:text="StartService"
android:id="@+id/staetservice"
/>
<Button
android:layout_width="match_parent"
android:onClick="doClick"
android:layout_height="wrap_content"
android:text="StopService"
android:id="@+id/stopservice"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bind:"
android:id="@+id/tv2"
/>
<Button
android:layout_width="match_parent"
android:onClick="doClick"
android:layout_height="wrap_content"
android:text="BindService"
android:id="@+id/bindservice"
/>
<Button
android:layout_width="match_parent"
android:onClick="doClick"
android:layout_height="wrap_content"
android:text="播放"
android:id="@+id/play"
/>
<Button
android:layout_width="match_parent"
android:onClick="doClick"
android:layout_height="wrap_content"
android:text="暂停"
android:id="@+id/stop"
/>
<Button
android:layout_width="match_parent"
android:onClick="doClick"
android:layout_height="wrap_content"
android:text="下一首"
android:id="@+id/next"
/>
<Button
android:layout_width="match_parent"
android:onClick="doClick"
android:layout_height="wrap_content"
android:text="上一首"
android:id="@+id/up"
/>
<Button
android:layout_width="match_parent"
android:onClick="doClick"
android:layout_height="wrap_content"
android:text="UnBindService"
android:id="@+id/unbindservice"
/>
</LinearLayout>
MainService.java
package com.example.xuhai.service;
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;
public class MainActivity extends AppCompatActivity {
Intent intent1;
Intent intent2;
MyBindService service;
ServiceConnection conn=new ServiceConnection() {
@Override//当启动源和Service成功连接后调用这个方法
public void onServiceConnected(ComponentName name, IBinder binder) {
service = ((MyBindService.MyBinder)binder).getService();
}
@Override// 当启动源跟Service的链接意外丢失的时候会调用这个方法比如当Service崩溃了或者被意外杀死
public void onServiceDisconnected(ComponentName name) {
}
};//一个代表与service链接状态的类,当我们连接service成功或失败时,会主动触发其内部的onServiceConnected()或onServiceDisconnected()方法。
//如果我们想要访问service中的数据,可以在onServiceConnection()方法中进行实现。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.service_layout);
}
public void doClick(View v){
switch(v.getId()){
case R.id.staetservice:
intent1 = new Intent(MainActivity.this,MyStartService.class);
startService(intent1);//启动一个service
break;
case R.id.stopservice:
stopService(intent1);//停止一个service
break;
case R.id.bindservice:
intent2 =new Intent(MainActivity.this,MyBindService.class);
//startService(intent1);
bindService(intent2,conn, Service.BIND_AUTO_CREATE);
break;
case R.id.play:
service.Play();
break;
case R.id.stop:
service.Stop();
break;
case R.id.next:
service.Next();
break;
case R.id.up:
service.Up();
break;
case R.id.unbindservice:
unbindService(conn);//只能解绑定一次
break;
}
}
/*
@Override
protected void onDestroy() {
stopService(intent1);
unbindService(conn);
super.onDestroy();
}*/
/**
* 在 Activity 中,我们通过 ServiceConnection 接口来取得建立连接 与 连接意外丢失的回调。bindService有三个参数,
* 第一个是用于区分 Service 的Intent 与 startService 中的 Intent 一致,第二个是实现了 ServiceConnection 接口的对象,
* 最后一个是 flag 标志位。有两个flag,BIND_DEBUG_UNBIND 与 BIND_AUTO_CREATE,前者用于调试(详细内容可以查看javadoc 上面描述的很清楚),
* 后者默认使用。unbindService 解除绑定,参数则为之前创建的 ServiceConnection 接口对象。另外,多次调用 unbindService 来释放相同的连接会抛出异常,
* 因此我创建了一个 boolean 变量来判断是否 unbindService 已经被调用过。
*/
}
MyStartService.java
package com.example.xuhai.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
/**
* Created by xuhai on 2016/8/6.
*/
public class MyStartService extends Service{
@Nullable
@Override
public void onCreate(){
super.onCreate();
Log.i("info", "onCreate()");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("info","onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("info", "onDestroy()");
}
public IBinder onBind(Intent intent) {
Log.i("info", "onBind()");
return null;
}
}
BindService.java
package com.example.xuhai.service;
import android.app.Service;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
/**
* Created by xuhai on 2016/8/6.
*/
public class MyBindService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i("info", "BindService---onBind()");
return new MyBinder();
}
public class MyBinder extends Binder{
public MyBindService getService(){
return MyBindService.this;
}
}
/**
* Local 服务的绑定较简单,首先在 Service 中我们需要实现 Service 的抽象方法 onBind,并返回一个实现 IBinder 接口的对象。
*/
@Override
public void onCreate() {
super.onCreate();
Log.i("info", "BindService---onCreate()");
}
@Override
public void unbindService(ServiceConnection conn) {
super.unbindService(conn);
Log.i("info", "BindService---unbinService()");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("info", "BindService---ondestroy");
}
/**
* 上面的代码关键之处,在于 onBind(Intent) 这个方法 返回了一个实现了 IBinder 接口的对象,这个对象将用于绑定Service 的 Activity 与 Local Service 通信
*/
public void Play(){
Log.i("info","播放");
}
public void Stop(){
Log.i("info","暂停");
}
public void Next(){
Log.i("info","下一首");
}<img src="https://img-blog.youkuaiyun.com/20160808193017767?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
public void Up(){
Log.i("info","上一首");
}
}
结果: