Android Studio Service非绑定(startservice)绑定式(bindService)

目录

    • Service的两种启动和销毁方式
      • 非绑定式startservice
      • 绑定式bindService
    • 基本用法
      • 非绑定式startservice(intent)
      • 绑定式 bindService(Intent service, ServiceConnection conn,int flags)
    • 实例代码测试启动过程
      • 测试图片

Service的两种启动和销毁方式

非绑定式startservice

一旦服务开启,就跟启动者没有任何关系,即使启动者退出,service依然在后台运行
启动者无法调用service里面的方法,但可以通过Intent传递参数,利用参数与service互动

绑定式bindService

绑定服务启动后,服务就跟启动者Activity绑定在一起,如果启动者销毁,服务也会跟着被销毁
启动者何以调用service里面的方法

基本用法

非绑定式startservice(intent)

1、新建MyService.class,继承Service类,重写onBind(Intent intent)、public void onCreate()、public int onStartCommand(Intent intent, int flags, int startId) 、onDestroy()方法
2、在AndroidManifest.xml清单文件中注册服务<service android:name=".MyService"/>
3、启动服务

Intent intent = new Intent(this, MyService.class);
        startService(intent);

绑定式 bindService(Intent service, ServiceConnection conn,int flags)

1、新建MyBindService.class,继承Service类,重写onBind(Intent intent)、onUnbind(Intent intent)、onDestroy()方法,自定已测试方法test()
2、在MyBindService类中新建子类MyBinder 继承 Binder,可以在MyBinder类中自定义方法,用于连接用户与服务

//Binder本身是实现了IBinder接口的
 public class MyBinder extends Binder {

        public void test() {
            Log.e("wksService","01:MyBinder.test()");
            MyBindService.test();
        }
    }

3、在MainActivity中实例化ServiceConnection连接对象,通过建立全局变量Binder调用service里面的方法

private MyBindService.MyBinder myBinder = null;
private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myBinder = (MyBindService.MyBinder) service;

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
    if(myBinder!=null){
		myBinder.test();
	}

4、在AndroidManifest.xml清单文件中注册服务<service android:name=".MyBindService" />
5、启动绑定服务

 Intent intent = new Intent(this, MyBindService.class);
        bindService(intent,conn,BIND_AUTO_CREATE);

实例代码测试启动过程

<!--清单文件注册Service-->
<service android:name=".MyService"/>
<service android:name=".MyBindService" />

//MyService.class
package com.example.myapplication;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

public class MyService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        Log.e("wksService","MyService.onCreate();");
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("wksService","MyService.onStartCommand(intent, flags, startId);");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        Log.e("wksService","非绑定式服务被销毁");
        super.onDestroy();
    }
}

//MyBindService.class
package com.example.myapplication;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class MyBindService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.e("wksService","MyBindService.onBind();\n服务绑定成功");
        return new MyBinder();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("wksService","MyBindService.onDestroy();");
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e("wksService","MyBindService.onUnbind();\n avtivity被销毁\nbind服务被解绑");
        return super.onUnbind(intent);
    }

    private static void test() {
        Log.i("wksService","02:MyBindService.test;\n通过Binder传递连接");
    }

    //用来连接用户与绑定服务
    public class MyBinder extends Binder {

        public void test() {
            Log.e("wksService","01:MyBinder.test()");
            MyBindService.test();
        }
    }
}
//MainActivityServiceTest.class
package com.example.myapplication;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivityServiceTest extends AppCompatActivity {
    
    private MyBindService.MyBinder myBinder = null;
    //服务连接器
    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myBinder = (MyBindService.MyBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_service_test);
    }
   
    //非绑定式startservice
    //一旦服务开启,就跟启动者没有任何关系,即使启动者退出,service依然在后台运行
    //启动者无法调用service里面的方法
    public void btn_click_service(View view) {
        Log.e("wksService", "服务启动中……");
        startService(view);
    }
    public void startService(View v) {

        Intent intent = new Intent(this, MyService.class);
        startService(intent);
    }
    //绑定式bindservice
    //绑定服务启动后,服务就跟启动者Activity绑定在一起,如果启动者销毁,服务也会跟着被销毁
    //启动者何以调用service里面的方法
    public void btn_click_bindservice(View view) {
        bindservice(view);
    }
    public void bindservice(View v){

        Intent intent = new Intent(this, MyBindService.class);
        bindService(intent,conn,BIND_AUTO_CREATE);
        //绑定服务之后调用绑定服务里面的方法
        if (myBinder!=null){
            myBinder.test();
        }
    }
}

测试图片

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值