码迷,mamicode.com
首页 > 系统相关 > 详细

activity与service进程内通信

时间:2016-07-31 17:30:17      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

package com.example.binbin.testbinder;

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

/**
 * Created by binbin on 2016/7/29.
 */
public class MyService extends Service {


    //自定义的binder,包含了我们所需的操作
    class myBinder extends Binder{

        //要在Service进行的操作
        public void sayHello(){

            Log.d("TAG","sayHello");
            Toast.makeText(MyService.this,"Hello",Toast.LENGTH_SHORT).show();

        }

        public void sayBye(){

            Log.d("TAG","sayBye");
            Toast.makeText(MyService.this,"Bye",Toast.LENGTH_SHORT).show();


        }


    };


    @Nullable
    @Override
    //绑定服务和进行通信的时候,一定要返回一个自定义的Binder
    public IBinder onBind(Intent intent) {
        Log.d("TAG","onBind");
        return new myBinder();
    }

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Log.d("TAG","onStartCommand");
        return super.onStartCommand(intent, flags, startId);

    }

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

    }

    @Override
    public boolean onUnbind(Intent intent) {

        Log.d("TAG","onUnbind");
        return super.onUnbind(intent);
    }
}

 

自定义了myBinder,在Activity与Service绑定的时候,返回myBinder的实例,Activity就可以操作Service里面的方法了(sayHello和sayBye)。

 

Activity里面必须要有两个实例,一个是myBinder,用于与Service通信的。另一个是ServiceConnection,代表了activity与服务的连接,在后面绑定的时候要传入这个类的实例,只有两个方法,我们要重写。第一个是连接成功时要干什么,肯定是要把binder返回给activity里面的binder啦。

 

public class MainActivity extends AppCompatActivity {

    //四个按钮
    Button start, stop, bind, unbind,sayHello,sayBye;

    //自定义的Binder对象
    private MyService.myBinder binder;

    //绑定服务要添加的对象,ServiceConnection代表与服务的连接,系统自己会调用,我们只需要实现,不需要显示调用
    //绑定的时候要把这个对象传进去。
    private ServiceConnection conn = new ServiceConnection() {

        //连接成功
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

            Log.d("TAG","Connect!");
            binder = (MyService.myBinder) service;  //获取其实例
            //binder.sayHello();                  //调用其方法
        }

        //连接失败
        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        start = (Button) findViewById(R.id.Start);
        stop = (Button) findViewById(R.id.Stop);
        bind = (Button) findViewById(R.id.Bind);
        unbind = (Button) findViewById(R.id.unBind);
        sayHello = (Button) findViewById(R.id.hello);
        sayBye = (Button) findViewById(R.id.bye);

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(MainActivity.this, MyService.class);
                startService(intent);

            }
        });

        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(MainActivity.this, MyService.class);
                stopService(intent);

            }
        });

        bind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(MainActivity.this,MyService.class);
                //传进去了conn对象
                bindService(intent,conn,BIND_AUTO_CREATE);

            }
        });

        unbind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //解除只需要传入连接对象
                unbindService(conn);

            }
        });

        sayHello.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                binder.sayHello();

            }
        });

        sayBye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                binder.sayBye();

            }
        });

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

 

开启和绑定服务之后,按Hello,就可以使用服务里面的sayHello方法了。

技术分享

activity与service进程内通信

标签:

原文地址:http://www.cnblogs.com/wzben/p/5723218.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!