之前记录过跨应用绑定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);

运行结果,如图,

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. 不吹不黑!GitHub 上帮助人们学习编码的 12 个资源,错过血亏...
  3. Android从服务器端接收json数据并解析的代码
  4. Android(安卓)Studio中常用快捷键
  5. [Android开发]Android(安卓)重要组件 之 Intent
  6. lua学习笔记 2 android调用Lua。Lua脚本中实现添加Button,并为Bu
  7. Android(安卓)容器控件
  8. Mac Android(安卓)Studio处理unable to access android sdk add-
  9. Android(安卓)onCreate方法被调用两次的解决方法

随机推荐

  1. 读取 android 设备的电池信息
  2. Android 中解析JSON形式的数据
  3. 史上最全谷歌Android开发工具Android Stu
  4. Android中级联列表ExpandableListView使
  5. 【Android-View】基于原生View的简单功能
  6. Android:制作Update.zip升级包
  7. [Android]后台Service 弹出自定义dialog
  8. Android快速开发框架之xUtils---图片模块
  9. 利用adb工具android真机环境运行cpp(无需r
  10. android 自定义ButtonTab , ActivityGrou