2021-06-17 Android Service


一、Service是什么?

Service则是Android提供一个允许长时间留驻后台的一个组件,执行长时间运行且不需要用户交互的任务。最常见的 用法就是做轮询操作,或者想在后台做一些事情,比如后台下载更新…服务包含的状态:
在这里插入图片描述

二、Service的生命周期

在这里插入图片描述
在这里插入图片描述

三、Service的使用

1.startService 与 stopService

设置两个按钮,分别是startService 与 stopService

    <!-- startService 对应 stopService 区域 -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="startService 对应 stopService 区域"
        android:textSize="20dp"
        android:textColor="@color/design_default_color_secondary"
        android:layout_gravity="center_horizontal"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="startServce"
            android:onClick="startServce"
            />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="stopServce"
            android:onClick="stopServce"
            />

    </LinearLayout>

对应的点击事件:

    /**
     * 跳转到第二个Activity 的事件函数
     * @param view
     */
    public void startActivity2(View view) {
        startActivity(new Intent(this, MainActivity2.class));
    }

    // =========================== startService 对应 stopService 区域
    // 启动服务
    public void startServce(View view) {
        startService(new Intent(this, MyService.class));
    }

    // 停止服务
    public void stopServce(View view) {
        stopService(new Intent(this, MyService.class));
    }

2.bindService 与unBindService

设置两个按钮,分别是bindService和unBindService

   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="bindService 对应 unBindService 区域"
        android:textSize="20dp"
        android:textColor="@color/black"
        android:layout_marginTop="100dp"
        android:layout_gravity="center_horizontal"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="bindServce"
            android:onClick="bindServce"
            />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="unBindServce"
            android:onClick="unBindServce"
            />

    </LinearLayout>

对应的点击事件:

    // =========================== bindService 对应 unBindService 区域

    public void bindServce(View view) {
        bindService(new Intent(this, MyService.class), connecton, Context.BIND_AUTO_CREATE);
    }

    public void unBindServce(View view) {
        unbindService(connecton);
    }

    // MainActivity 与 MyService 的桥梁
    private ServiceConnection connecton = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    // 一般的写法,当此Activity被销毁的时候,自动解绑服务
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connecton);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值