安卓夸进程通信

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/likui19921224/article/details/79174564
       跨进程通讯是基于两款app而言  我们可以在当前app调用另外一个app的服务
       先简单介绍点服务:
          博文里有一些服务的介绍 这里简单说下:
          1.服务是运行在主线程中
          2.前台服务使用只需要在创建时也就是oncreate中写入一下代码就可以了

// Notification nocation=new Notification(R.drawable.ic_launcher,
// “Karin的程序”, System.currentTimeMillis());
// Intent in=new Intent(this,mServiceActivity.class);
// PendingIntent pen=PendingIntent.getActivity(this, 0, in, 0);
// nocation.setLatestEventInfo(this,
// “收到消息”, “haha”, pen);
// startForeground(1, nocation);
// Log.e(“当前进程”, Process.myPid()+”“);

3.远程服务的声明: manifest中声明服务:process=”:remote”
4.远程服务不能在代码中出现和activity相关的东西 (使用服务绑定的时候),绑定时候就会出现 会直接崩溃
5.远程服务通信AIDL 在AIdl中定义接口 如果在gen下能够显示相同文件.java 创建正确
6.跨进程服务通讯
假设两个appA,B 只需要在B中写下服务和AIDL接口文档 同上面第五点
并声明B服务为远程
A中使用B中服务的action绑定就可以了
//展示下:代码:
B:

@Override
public IBinder onBind(Intent intent) {

    return m;
}
mAIDL.Stub  m=new Stub() {

    @Override
    public String toUpperCase(String str) throws RemoteException {

        return str.toUpperCase();
    }

    @Override
    public int plus(int a, int b) throws RemoteException {

        return a+b;
    }
};

//b中AIDL 也就是mAIDL.aidl文件内容

package com.Karins.Activity;
interface mAIDL {
int plus(int a, int b);
String toUpperCase(String str);
}

//b中服务配置:

service android:name=”com.Karins.Activity.CommunicationService”
android:process=”:remote”
intent-filter
action android:name=”com.Karins.Activity.CommunicationService”
intent-filter
service

A中:
同样把B中的配置和包复制过来 ,单不要复制服务(复制过来无所谓 )
在A中绑定服务直接使用intent的隐私启动就可以了

猜你喜欢

转载自blog.csdn.net/likui19921224/article/details/79174564