zoukankan      html  css  js  c++  java
  • android90 bind方式启动服务service调用service里的方法

    package com.itheima.banzheng;
    
    import com.itheima.banzheng.LeaderService.ZhouMi;
    
    import android.os.Bundle;
    import android.os.IBinder;
    import android.app.Activity;
    import android.content.ComponentName;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.view.Menu;
    import android.view.View;
    
    public class MainActivity extends Activity {
    
        private Intent intent;
        private MyServiceConn conn;
        PublicBusiness pb;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            intent = new Intent(this, LeaderService.class);
            conn = new MyServiceConn();//服务连接对象,成功后就调用
            //绑定领导服务
            bindService(intent, conn, BIND_AUTO_CREATE);//执行这个代码后服务就成功的绑定了连接就建立了,连接后会调用LeaderService的onBind方法返回IBinder对象,
        }
        
        public void click(View v){
            //调用服务的办证方法
            pb.QianXian();//这样就可以调用LeaderService的banZheng方法,否则是不能调用LeaderService的banZheng方法的
        }
    
        class MyServiceConn implements ServiceConnection{//服务连接对象
            //连接服务成功(成功拿到中间人IBinder的ZhouMi对象),此方法调用
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                //service就是中间人ZhouMi
                pb = (PublicBusiness) service;
            }
    
            @Override
            public void onServiceDisconnected(ComponentName name) {
            }
        }
    }
    Service:
    package com.itheima.banzheng; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder;
    public class LeaderService extends Service { @Override public IBinder onBind(Intent intent) { // 返回一个IBinder对象,这个对象就是MainActivity和LeaderService之间的中间人对象 return new ZhouMi();//返回给了onServiceConnected的service参数 } class ZhouMi extends Binder implements PublicBusiness{ public void QianXian(){ banZheng(); } public void daMaJiang(){ System.out.println("陪李处打麻将"); } } public void banZheng(){ System.out.println("李处帮你来办证"); } }

    接口:

    package com.itheima.banzheng;
    
    public interface PublicBusiness {
    
        void QianXian();
    }

    清单文件:

    <service android:name="com.itheima.banzheng.LeaderService"></service>

    音乐播放器:

    package com.itheima.musicplayer;
    
    import android.os.Bundle;
    import android.os.IBinder;
    import android.app.Activity;
    import android.content.ComponentName;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.view.Menu;
    import android.view.View;
    
    public class MainActivity extends Activity {
    
        MusicInterface mi;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Intent intent = new Intent(this, MusicService.class);
            //混合调用
            //为了把服务所在进程变成服务进程
            startService(intent);
            //为了拿到中间人对象
            bindService(intent, new MusicServiceConn(), BIND_AUTO_CREATE);
            //* 服务的混合调用
            //* 先开始、再绑定,先解绑、再停止
        }
    
        class MusicServiceConn implements ServiceConnection{
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                mi = (MusicInterface) service;//返回中间人对象
            }
            @Override
            public void onServiceDisconnected(ComponentName name) {
            }
            
        }
    
        //开始播放按钮
        public void play(View v){//Activity的方法
            mi.play();//中间人的方法,调用Service的方法
        }
        //暂停播放按钮
        public void pause(View v){
            mi.pause();
        }
    }
    //清单文件: <service android:name="com.itheima.musicplayer.MusicService"></service>
    package com.itheima.musicplayer;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.Binder;
    import android.os.IBinder;
    
    public class MusicService extends Service{
        @Override
        public IBinder onBind(Intent intent) {
            return new MusicController();//中间人对象
        }
        
        //必须继承binder,才能作为中间人对象返回给onServiceConnected的service
        class MusicController extends Binder implements MusicInterface{
            public void play(){
                MusicService.this.play();//中间人对象调用Service的方法
            }
            public void pause(){
                MusicService.this.pause();
            }
        }
        
        public void play(){//Service的方法
            System.out.println("播放音乐");
        }
        
        public void pause(){
            System.out.println("暂停播放");
        }
    
        
    }
    package com.itheima.musicplayer;
    
    public interface MusicInterface {
    
        void play();
        void pause();
    }
    #找领导办证
    * 把服务看成一个领导,服务中有一个banZheng方法,如何才能访问?
    * 绑定服务时,会触发服务的onBind方法,此方法会返回一个Ibinder的对象给MainActivity,通过这个对象访问服务中的方法
    * 绑定服务
    
            Intent intent = new Intent(this, BanZhengService.class);
            bindService(intent, conn, BIND_AUTO_CREATE);
    * 绑定服务时要求传入一个ServiceConnection实现类的对象
    * 定义这个实现类
    
            class MyServiceconn implements ServiceConnection{
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                zjr = (PublicBusiness) service;
            }
            @Override
            public void onServiceDisconnected(ComponentName name) {    
            }
            
        }
    * 创建实现类对象
    
             conn = new MyServiceconn();
    * 在服务中定义一个类实现Ibinder接口,以在onBind方法中返回
    
            class ZhongJianRen extends Binder implements PublicBusiness{
            public void QianXian(){
                //访问服务中的banZheng方法
                BanZheng();
            }    
            public void daMaJiang(){
                
            }
        }
    * 把QianXian方法抽取到接口PublicBusiness中定义
    
    ---
    #两种启动方法混合使用
    * 用服务实现音乐播放时,因为音乐播放必须运行在服务进程中,可是音乐服务中的方法,需要被前台Activity所调用,所以需要混合启动音乐服务
    * 先start,再bind,销毁时先unbind,在stop
  • 相关阅读:
    一个IT工薪族的4年奋斗成果
    一个IT工薪族的4年奋斗成果
    【VBA】制作散点图及打标签VBA
    【VBA】合并多个excel文件
    【统计分析】ROC曲线
    【ARDUINO】HC-05蓝牙不配对问题
    【ARDUINO】串口无法打开问题
    【ARDUINO】蓝牙(HC-05)透传
    【Raspberry pi+python+mysql】红外传感器-发邮件-存数据库
    【Raspberry Pi】crontab 定时任务
  • 原文地址:https://www.cnblogs.com/yaowen/p/4951823.html
Copyright © 2011-2022 走看看