Android -- 跨应用绑定service并通信

之前记录过跨应用绑定service:http://blog.csdn.net/gaopeng0071/article/details/46049929,那么绑定后如何进行通信呢,下面我们就来学习下。

第一步,
需要修改service1项目中aidl,增加一个方法。

package com.example.service1.aidl;  

interface IMyService {  

    void basicType();

    void setName(String name);
}

setName用于存储name的方法。
然后clear项目

第二步,
此时,我们service类中的onBind方法需要实现新接口。

package com.example.service1;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

import com.example.service1.aidl.IMyService;

public class MyService extends Service {

    private String serviceName = "默认名字";
    private boolean running;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return new IMyService.Stub() {

            @Override
            public void basicType() throws RemoteException {
                // TODO Auto-generated method stub

            }

            @Override
            public void setName(String name) throws RemoteException {
                serviceName = name;
            }
        };
    }

    @Override
    public void onCreate() {
        running = true;
        new Thread() {
            public void run() {
                while (running) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println(serviceName);
                }
            };
        }.start();
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        running = false;
    }

}

代码27到29行,接收到name并且放入全局变量中,提供给onCreate方法输出。

第三步,
将service1项目中aidl拷贝到service2项目中,并且包名要一致,
这里写图片描述

第四步,
修改service2应用activity布局,增加一个text域,和一个按钮。用于将text中的信息提交到service1项目的service中。
这里写图片描述

第五步,
修改service2项目中activity,增加与service1的通信,

package com.example.service2;

import com.example.service1.aidl.IMyService;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener,
        ServiceConnection {

    Intent serviceIntent;

    private IMyService imyService = null;

    TextView t;

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

        serviceIntent = new Intent();
        serviceIntent.setComponent(new ComponentName("com.example.service1",
                "com.example.service1.MyService"));

        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);
        findViewById(R.id.button4).setOnClickListener(this);

        findViewById(R.id.button5).setOnClickListener(this);

        t = (TextView) findViewById(R.id.textView1);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1:
            startService(serviceIntent);
            break;
        case R.id.button2:
            stopService(serviceIntent);
            break;
        case R.id.button3:
            bindService(serviceIntent, this, Context.BIND_AUTO_CREATE);
            break;
        case R.id.button4:
            unbindService(this);
            imyService = null;
            break;
        case R.id.button5:
            if (imyService != null) {
                try {
                    imyService.setName(t.getText().toString());
                } catch (RemoteException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        imyService = IMyService.Stub.asInterface(service);
        System.out.println("onServiceConnected");
        System.out.println(service);

    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        // TODO Auto-generated method stub

    }

}

代码72行,传输数据
代码84行用法imyService = IMyService.Stub.asInterface(service);

运行结果,如图,
这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值