使用AIDL跨进程调用_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3052 | 回复: 0   主题: 使用AIDL跨进程调用        下一篇 
java_along
注册用户
等级:上尉
经验:771
发帖:36
精华:0
注册:2014-2-17
状态:离线
发送短消息息给java_along 加好友    发送短消息息给java_along 发消息
发表于: IP:您无权察看 2014-8-4 23:09:49 | [全部帖] [楼主帖] 楼主

所谓跨进程就是说2个不同的应用。

这个应用调用另外一个应用里面的服务。

使用aidl,即Android Interface Definition Language,Android接口定义语言。

跟之前的绑定Service的方法差不多。

使用aidl方式的话可以进行跨进程调用Service.

先简单写一个aidl文件:其实可以先写一个java接口

比如:

package com.example.activity02;
public interface IStudentService {
      String query(int index);
}


然后把java后缀名改为.aidl,发现会出错,因为aidl的语法,interface前面不用加public 

所以正确的应该是:

package com.example.activity02;
interface IStudentService {
      String query(int index);
}


当你写好后,会发现在gen的文件夹中的对应包中会有IStudentService的类,这是自动生成的。

接下来就是

写一个Service:

package com.example.activity02;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class StudentService extends Service{
private String[] arr=new String[]{"刘备","关羽","张飞"};
      private String queryName(int index){
            return arr[index];
      }
      @Override
      public IBinder onBind(Intent arg0) {
            return new StudentBinder();
      }
      class StudentBinder extends IStudentService.Stub{
            @Override
            public String query(int index) throws RemoteException {
                  return queryName(index);
            }
      }
}


这个Service的代码跟之前的帖子是差不多的,只是之前的帖子,StudentBinder是先继承Binder然后再实现自定义接口,IstudentService.Stub是系统自动帮我们生成的抽象类,继承了Binder对象,也实现了我们刚才在aidl文件中定义的接口。

前面的内容是一个应用,下面我们新建另外一个应用。

复制在前面应用中写的aidl文件。

然后我们也像前面的那个帖子一样调用query 查询信息。

Activity:
package com.example.activity02;
import com.example.android02.R;
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.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Activity04 extends Activity{
      private EditText text1=null;
      private TextView text2=null;
      private Button btn=null;
      private IStudentService service=null;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            text1=(EditText)findViewById(R.id.text);
            text2=(TextView)findViewById(R.id.text2);
            btn=(Button)findViewById(R.id.btn1);
            Intent intent=new Intent();
            intent.setAction("abcdefg");
            bindService(intent, new StudentServiceConnection(), Context.BIND_AUTO_CREATE);
            btn.setOnClickListener(new OnClickListener() {
                  @Override
                  public void onClick(View arg0) {
                        try {
                              text2.setText(service.query(Integer.parseInt(text1.getText().toString())));
                        } catch (NumberFormatException e) {
                              e.printStackTrace();
                        } catch (RemoteException e) {
                              e.printStackTrace();
                        }
                  }
            });
      }
      class StudentServiceConnection implements ServiceConnection{
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder binder) {
                  service= IStudentService.Stub.asInterface(binder);
            }
            @Override
            public void onServiceDisconnected(ComponentName componectName) {
                  service=null;
            }
      }
}


内容跟前面的activity差不多。

需要注意的是,因为是跨应用,所以就不好直接用setClass 这样的显示Intent去调用Service

可以使用setAction addCategory等隐式Intent去调用另一个应用的Service

在上个应用中 我是这么配置service的。

<service android:name="com.example.activity02.StudentService">
<intent-filter>
<action android:name="abcdefg"/>
</intent-filter>
</service>


还有个值得注意的是,在上一个帖子里,我们是通过自定义接口来接收得到的IBinder,使用aidl后,可以直接调用IStudentService.Stub.asInterface 这个方法得到接口,然后调用方法。

效果:

北京联动北方科技有限公司




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论