接口实现服务Service_暴露需要暴露的方法

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

https://blog.csdn.net/weixin_40790006/article/details/79960181

服务Service类:

    只有绑定服务时,取消绑定,会销毁服务。混合开启服务,不会

package com.example.f405.iserviceaidltest.utils;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by F405 on 2018/4/16.
 */

public class MyTestService extends Service {

    private static final String TAG = "MyTestService";

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate: ----onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand: ----onStartCommand");

        return super.onStartCommand(intent, flags, startId);

    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind: ----onBind");

        // 返回自定义的Binder类型
        return new MyBinder();
    }

    private class MyBinder extends Binder implements IService{

        // TODO 调用服务里的方法,通过接口,暴露需要暴露的方法
        public void callStartMusic(){
            startMusic();
        }

        public void callPauseMusic(){
            pauseMusic();
        }

        public void callStopMusic(){
            stopMusic();
        }

        // 不暴露的方法
        public void callLyrics(){
            lyrics();
        }

        public void callsinger(){
            singer();
        }

    }

    public void startMusic(){
        Toast.makeText(MyTestService.this, "start", Toast.LENGTH_SHORT).show();
    }
    public void pauseMusic(){
        Toast.makeText(MyTestService.this, "pause", Toast.LENGTH_SHORT).show();

    }
    public void stopMusic(){
        Toast.makeText(MyTestService.this, "stop", Toast.LENGTH_SHORT).show();

    }
    // 不对外暴露的方法
    public void  lyrics(){
        Toast.makeText(MyTestService.this, "lyrics", Toast.LENGTH_SHORT).show();

    }
    public void singer(){
        Toast.makeText(MyTestService.this, "singer", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy:---- onDestroy");

        super.onDestroy();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值