绑定解绑服务并调用服务内部方法

MainActivity

//绑定服务
    public void bindService(View view) {
        Intent intent=new Intent();
        intent.setClass(this,FristService.class);
        bindService= bindService(intent, mConnection, BIND_AUTO_CREATE);

    }
    private ServiceConnection mConnection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            myIbinder= (FristService.MyIbinder) iBinder;

        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            myIbinder=null;
        }
    };
    //解绑服务
    public void unBindService(View view) {
        if (mConnection!=null&&bindService){
            unbindService(mConnection);
        }
    }
    //调用服务内部方法
    public void callService(View view) {
        myIbinder.callService();
    }

完整

package ml.yompc.servicedemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;

import ml.yompc.servicedemo.service.FristService;

public class MainActivity extends AppCompatActivity {
    boolean bindService;
    FristService.MyIbinder myIbinder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    //开启服务
    public void startService(View view) {
        Intent intent=new Intent();
        intent.setClass(this,FristService.class);
        startService(intent);
    }
    //停止服务
    public void stopService(View view) {
        Intent intent=new Intent();
        intent.setClass(this,FristService.class);
        stopService(intent);
    }

    //绑定服务
    public void bindService(View view) {
        Intent intent=new Intent();
        intent.setClass(this,FristService.class);
        bindService= bindService(intent, mConnection, BIND_AUTO_CREATE);

    }
    private ServiceConnection mConnection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            myIbinder= (FristService.MyIbinder) iBinder;

        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            myIbinder=null;
        }
    };
    //解绑服务
    public void unBindService(View view) {
        if (mConnection!=null&&bindService){
            unbindService(mConnection);
        }
    }
    //调用服务内部方法
    public void callService(View view) {
        myIbinder.callService();
    }
}

FristService

public class MyIbinder extends Binder {
        public void callService(){
            sayHi();
        }

    }
public void sayHi(){
        Toast.makeText(getApplicationContext(),"Hello World",Toast.LENGTH_SHORT).show();
    }

完整

package ml.yompc.servicedemo.service;

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

import androidx.annotation.Nullable;

/**
 * @email yom535@outlook.com
 * @author: 有民(yom535)
 * 学号:1703010141
 * @date: 2019/11/5
 * @time: 16:02
 */
public class FristService extends Service {
    private final String TAG=FristService.class.getName();

    public class MyIbinder extends Binder {
        public void callService(){
            sayHi();
        }

    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MyIbinder();
    }
    //服务创建时
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG,"onCreate....");
    }
    //服务结束时
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"onDestroy....");
    }
    //服务启动时
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG,"onStartCommand....");
        return super.onStartCommand(intent, flags, startId);
    }

    public void sayHi(){
        Toast.makeText(getApplicationContext(),"Hello World",Toast.LENGTH_SHORT).show();
    }
}