前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >3-AIII--Service绑定服务基于接口调用方法

3-AIII--Service绑定服务基于接口调用方法

作者头像
张风捷特烈
发布2018-09-26 16:55:41
6450
发布2018-09-26 16:55:41
举报
文章被收录于专栏:Android知识点总结
零、前言

1.上篇中MyBinder暴漏在外,亲自上阵,执行方法。Binder是连接者,能不出面尽量不出面,一个"电话(接口)"搞定的事就不要让他老人家(MyBinder)亲自跑一趟。面向接口,隐藏不必要的对象。 2.说服务怎能少得了音乐播放器,这里只写写伪方法,后面在多媒体篇会来完善。

基于接口调用方法.gif

一、代码实现
1.服务方法接口
代码语言:javascript
复制
/**
 * 作者:张风捷特烈
 * 时间:2018/8/25 0025:11:09
 * 邮箱:1981462002@qq.com
 * 说明:服务方法接口
 */
public interface IMService {
    /**
     * 播放音乐
     */
    void playMusic();

    /**
     * 暂停音乐
     */
    void pauseMusic();

    /**
     * 继续播放音乐
     */
    void rePlayMusic();

}
2.音乐服务:MusicService
代码语言:javascript
复制
public class MusicService extends Service {
    private static final String TAG = "MusicService";
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MusicBinder();
    }
    
    private class MusicBinder extends Binder implements IMService {

        @Override
        public void playMusic() {
            Log.e(TAG, "playMusic: ");
        }

        @Override
        public void pauseMusic() {
            Log.e(TAG, "pauseMusic: ");

        }

        @Override
        public void rePlayMusic() {
            Log.e(TAG, "rePlayMusic: ");
        }
    }
}
3.MusicActivity
代码语言:javascript
复制
public class MusicActivity extends Activity {

    @BindView(R.id.btn_music_play)
    Button mBtnMusicPlay;
    @BindView(R.id.btn_music_stop)
    Button mBtnMusicStop;
    @BindView(R.id.btn_music_continue)
    Button mBtnMusicContinue;
    private ServiceConnection mConn;
    private IMService mImservice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music);
        ButterKnife.bind(this);

        bindMusic();
    }

    private void bindMusic() {
        // [0]保证服务长期运行
        Intent intent = new Intent(this, MusicService.class);
        startService(intent);

        // [1]调用bindservice 目的是为了获取我们定义的中间人对象
        mConn = new ServiceConnection() {
            @Override // 当连接成功时候调用
            public void onServiceConnected(ComponentName name, IBinder service) {
                mImservice = (IMService) service;// 获取我们定义的中间人对象
            }

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

        // 连接MusicService 服务 获取我们定义的中间人对象
        bindService(intent, mConn, BIND_AUTO_CREATE);

    }


    @OnClick({R.id.btn_music_play, R.id.btn_music_stop, R.id.btn_music_continue})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_music_play: // 播放音乐
                mImservice.playMusic();
                break;
            case R.id.btn_music_stop:// 暂停音乐
                mImservice.pauseMusic();
                break;
            case R.id.btn_music_continue:// 继续音乐
                mImservice.rePlayMusic();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        // 当activity销毁的时候 取消绑定服务
        if (mConn != null) {
            unbindService(mConn);
        }
        super.onDestroy();
    }

}
附录、布局文件:activity_music.xml
代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btn_music_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="音乐播放" />

    <Button
        android:id="@+id/btn_music_stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="音乐暂停" />

    <Button
        android:id="@+id/btn_music_continue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="音乐继续" />

</LinearLayout>

本文由张风捷特烈原创,转载请注明 更多安卓技术欢迎访问:https://www.jianshu.com/c/004f3fe34c94 张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com 你的喜欢与支持将是我最大的动力

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018.08.25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 零、前言
  • 一、代码实现
    • 1.服务方法接口
      • 2.音乐服务:MusicService
        • 3.MusicActivity
          • 附录、布局文件:activity_music.xml
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档