作为android四大组件之一的service经常被用做后台任务,比如后台播放音乐。但是经常有在页面中调用service中方法的需求。比如后台播放音乐,在前台暂停,开始,快进等操作,需要在activity中调用本地service中的方法
先来说一下service的启动方式。service的启动方式有以下几种
- 用startService()启动本地服务。
- 用bindService()启动服务。
- 混合方式启动本地服务。
这三种方式启动service有什么区别呢?
- startService()启动服务无法拿到service对象,就无法调用service中的方法。
- bindService()启动服务可以通过ServiceConnection去操作服务中的方法,但是这样启动的服务当启动它的activity关闭时,service会跟着关闭。
- 混合方式是先用startService()方式启动服务然后用bindService()绑定服务可以访问服务中的方法,activity退出时service不会关闭。
下面以混合方式为例实现该功能
代码如下
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.zl.itgeek.servicedemo.MainActivity"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="a"
android:onClick="a"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="b"
android:onClick="b"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="c"
android:onClick="c"/>
</LinearLayout>
activity
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 {
MyService.MyBinder myBinder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this,MyService.class);
startService(intent);
bindService(intent,new Connect(),0);
}
public void a(View v){
myBinder.toA();
}
public void b(View v){
myBinder.toB();
}
public void c(View v){
myBinder.toC();
}
class Connect implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myBender = (MyService.MyBinder) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
}
service
package com.zl.itgeek.servicedemo;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
public MyService() {
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this,"服务启动了",Toast.LENGTH_SHORT);
}
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
class MyBinder extends Binder{
public void toA(){
a();
}public void toB(){
b();
}public void toC(){
c();
}
}
public void a(){
Toast.makeText(this,"a",Toast.LENGTH_SHORT);
Log.e("com.deubg.zl","a方法执行成功了");
}
public void b(){
Toast.makeText(this,"b",Toast.LENGTH_SHORT);
Log.e("com.deubg.zl","b方法执行成功了");
}
public void c(){
Toast.makeText(this,"c",Toast.LENGTH_SHORT);
Log.e("com.deubg.zl","c方法执行成功了");
}
}
onBind()方式绑定只需要将上面的startService(intent);删掉就行了。