zoukankan      html  css  js  c++  java
  • 如何从Activity中调用Service中的方法

    原文链接http://stackoverflow.com/questions/2272378/android-using-method-from-a-service-in-an-activity

    (关于IBinder:官方说Base interface for a remotable object, the core part of a lightweight remote procedure call mechanism designed for high performance when performing in-process and cross-process calls.)

    There are 3 ways to binding service with your activity.

    1. IBinder Implementation
    2. Using Messanger
    3. Using AIDL

    这里了解一下用IBinder的方法.

     Server.java Service:

    public class Server extends Service{
    
        IBinder mBinder = new LocalBinder();
    
    
        @Override
        public IBinder onBind(Intent intent) {
            return mBinder;
        }
    
        public class LocalBinder extends Binder {
            public Server getServerInstance() {
                return Server.this;
            }
        }
    
        public void switchSpeaker(boolean speakerFlag){
    
            if(speakerFlag){
            audio_service.setSpeakerphoneOn(false);
            }
            else{
            audio_service.setSpeakerphoneOn(true);
            }
    
        }
    }

    Client.java Activity:

    public class Client extends Activity {
    
    boolean mBounded;
    Server mServer;
    TextView text;
    Button button;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    text = (TextView)findViewById(R.id.text);
    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener() {
    
            public void onClick(View v) {
                mServer.switchSpeaker(true);
            }
        });
    
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        Intent mIntent = new Intent(this, Server.class);
    bindService(mIntent, mConnection, BIND_AUTO_CREATE);
    };
    
    ServiceConnection mConnection = new ServiceConnection() {
    
        public void onServiceDisconnected(ComponentName name) {
            Toast.makeText(Client.this, "Service is disconnected", 1000).show();
            mBounded = false;
            mServer = null;
        }
    
        public void onServiceConnected(ComponentName name, IBinder service) {
            Toast.makeText(Client.this, "Service is connected", 1000).show();
            mBounded = true;
            LocalBinder mLocalBinder = (LocalBinder)service;
            mServer = mLocalBinder.getServerInstance();
        }
    };
    
    @Override
    protected void onStop() {
        super.onStop();
        if(mBounded) {
            unbindService(mConnection);
            mBounded = false;
        }
    };
    }
  • 相关阅读:
    SpringBoot简单项目学习笔记08(servlet的内置容器的切换(tomcat、jetty、undertow))
    读《大家看的设计书(第三版)》有感
    百度比赛任务二收获
    百度前端任务一学习的知识
    学习git的内容
    codeforces #588 ABCD
    codeforces #597 div2 ABCD !F
    codeforces #589 div2 ABCD E待补
    codeforces #590 div3 BCDF E待补
    codeforces #591 div2 ABCD
  • 原文地址:https://www.cnblogs.com/linxiaojiang/p/3171949.html
Copyright © 2011-2022 走看看