美文网首页
service总结

service总结

作者: couriravant | 来源:发表于2020-01-02 19:19 被阅读0次

启动方式:

  1. 启动:
    生命周期: onCreate() -> onStartCommand() -> onDestory()
    Activity销毁后service继续运行,不能调用service。
  2. 绑定:onCreate() -> onBind() -> onUnBind() -> onDestory()
    Activity销毁后service销毁,绑定者可以调用服务里面的方法。

到这里,两种方式的区别已经明确了。

问题来了。
绑定者如何调用服务里的方法呢?

首先定义一个Service的子类。

public class MyService extends Service {

    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        //返回MyBind对象
        return new MyBinder();
    }

    private void methodInMyService() {
        Toast.makeText(getApplicationContext(), "服务里的方法执行了。。。",
                Toast.LENGTH_SHORT).show();
    }

    /**
     * 该类用于在onBind方法执行后返回的对象,
     * 该对象对外提供了该服务里的方法
     */
    private class MyBinder extends Binder implements IMyBinder {

        @Override
        public void invokeMethodInMyService() {
            methodInMyService();
        }
    }
}

自定义的MyBinder接口用于保护服务中不想让外界访问的方法。

public interface IMyBinder {

     void invokeMethodInMyService();

}

接着在Manifest.xml文件中配置该Service

<service android:name=".MyService"/>

在Activity中绑定并调用服务里的方法
简单布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="start"
        android:text="开启服务"
        android:textSize="30sp" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="invoke"
        android:text="调用服务的方法"
        android:textSize="30sp" />
</LinearLayout>

绑定服务的Activity:

public class MainActivity extends Activity {

private MyConn conn;
private Intent intent;
private IMyBinder myBinder;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

//开启服务按钮的点击事件
public void start(View view) {
    intent = new Intent(this, MyService.class);
    conn = new MyConn();
    //绑定服务,
    // 第一个参数是intent对象,表面开启的服务。
    // 第二个参数是绑定服务的监听器
    // 第三个参数一般为BIND_AUTO_CREATE常量,表示自动创建bind
    bindService(intent, conn, BIND_AUTO_CREATE);
}

//调用服务方法按钮的点击事件
public void invoke(View view) {
    myBinder.invokeMethodInMyService();
}

private class MyConn implements ServiceConnection {
    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        //iBinder为服务里面onBind()方法返回的对象,所以可以强转为IMyBinder类型
        myBinder = (IMyBinder) iBinder;
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
    }
}

}
绑定本地服务调用方法的步骤:

在服务的内部创建一个内部类 提供一个方法,可以间接调用服务的方法
实现服务的onbind方法,返回的就是这个内部类
在activity 绑定服务。bindService();
在服务成功绑定的回调方法onServiceConnected, 会传递过来一个 IBinder对象
强制类型转化为自定义的接口类型,调用接口里面的方法

refer:https://www.jianshu.com/p/2fb6eb14fdec

相关文章

  • 学习总结:WorkShop(一)

    学习总结:WorkShop(一) 基于RestfulAPI的Service服务-Micro-Service to ...

  • ubuntu中的定时任务——crontab

    1.命令总结 service cron start //启动服务 service cron stop ...

  • Service总结

    什么是serice: Service可由其他应用组件启动,而且即使用户切换到其他应用,服务仍将在后台继续运行。 此...

  • service总结

    纯粹是个人学习总结,如有不对的地方请吐槽。 Service是一种在Android应用后台的一种组件,没有自己的界面...

  • service总结

    相关文章 【1】 service简单使用 本篇文章很庞大,讲述了service的基本概念,两种启动方式的比较,以及...

  • service总结

    启动方式: 启动:生命周期: onCreate() -> onStartCommand() -> onDestor...

  • 【面试题】Service的开启方式

    参考:Android Service两种启动方式详解(总结版)Service的两种启动方式

  • Android:Service 总结

    Service是android的四大组件之一,相对与Activity,他没有界面,可以在后台运行。 问题1:ser...

  • Service aidl 总结

    Service两种启动方式 startService 启动流程oncreate->onStartCommand->...

  • Service知识总结

    1. 概述 想必做过Android开发的同学应该都用过Service,然而有木有想过比较全面的掌握Service相...

网友评论

      本文标题:service总结

      本文链接:https://www.haomeiwen.com/subject/czixoctx.html