• 如何从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;
        }
    };
    }
  • 相关阅读:
    Linux下SSH的Log文件路径
    Linux下压缩与解压命令tar
    Linux命令之at
    Linux下nice/renice命令小结
    Linux命令详解nice
    LVM---动态调整磁盘容量
    VT100字体
    Linux命令之WC
    for name in loop Shell
    Bind9用view配主从
  • 原文地址:https://www.cnblogs.com/linxiaojiang/p/3171949.html
Copyright © 2020-2023  润新知