Android中在native层对java层应用程序发送广播方法及原理

Android中在native层对java层应用程序发送广播方法及原理详解

Android从native发送数据给上层应用接收,常用的有JNI的形式,同样用广播也可以实现,本文正是基于广播实现native与上层应用通信,参考Android的Native方式广播intent,但是仅仅知道方法是不够的,需要更深层次的去了解数据是如何传输的原理,本文分两个部分,先讲发送广播的方法,再讲为何要按照这种格式发送。

本地广播发送方法

发送端

这是vendor\mediatek\proprietary\packages\apps\MTKThermalManager\jni\thermald.cpp中的源码,发送一个指定Action,并携带一个key为type,值为value的数据的广播。

bool sendBroadcastMessage(String16 action, int value)
{
TM_INFO_LOG("sendBroadcastMessage(): Action: %s, Value: %d ", action.string(), value);
sp<IServiceManager> sm = defaultServiceManager();
sp<IBinder> am = sm->getService(String16("activity"));
if (am != NULL) {
    Parcel data, reply;
    data.writeInterfaceToken(String16("android.app.IActivityManager"));
    data.writeStrongBinder(NULL);
    // intent begin
    data.writeString16(action); // action
    data.writeInt32(0); // URI data type
    data.writeString16(NULL, 0); // type
    data.writeInt32(0); // flags
    data.writeString16(NULL, 0); // package name
    data.writeString16(NULL, 0); // component name
    data.writeInt32(0); // source bound - size
    data.writeInt32(0); // categories - size
    data.writeInt32(0); // selector - size
    data.writeInt32(0); // clipData - size
    data.writeInt32(-2); // contentUserHint: -2 -> UserHandle.USER_CURRENT
    data.writeInt32(-1); // bundle extras length
    data.writeInt32(0x4C444E42); // 'B' 'N' 'D' 'L'
    int oldPos = data.dataPosition();
    data.writeInt32(1);  // size
    // data.writeInt32(0); // VAL_STRING, need to remove because of analyze common intent 
    data.writeString16(String16("type"));
    data.writeInt32(1); // VAL_INTEGER
    data.writeInt32(value);
    int newPos = data.dataPosition();
    data.setDataPosition(oldPos - 8);
    data.writeInt32(newPos - oldPos); // refill bundle extras length
    data.setDataPosition(newPos);
    // intent end
    data.writeString16(NULL, 0); // resolvedType
    data.writeStrongBinder(NULL); // resultTo
    data.writeInt32(0); // resultCode
    data.writeString16(NULL, 0); // resultData
    data.writeInt32(-1); // resultExtras
    data.writeString16(NULL, 0); // permission
    data.writeInt32(0); // appOp
    data.writeInt32(-1); // option
    data.writeInt32(1); // serialized: != 0 -> ordered
    data.writeInt32(0); // sticky
    data.writeInt32(-2); // userId: -2 -> UserHandle.USER_CURRENT

    status_t ret = am->transact(IBinder::FIRST_CALL_TRANSACTION + 13, data, &reply); // BROADCAST_INTENT_TRANSACTION
    if (ret == NO_ERROR) {
        int exceptionCode = reply.readExceptionCode();
        if (exceptionCode) {
            TM_INFO_LOG("sendBroadcastMessage(%s) caught exception %d\n",
                        action.string(), exceptionCode);
            return false;
        }
    } else {
        return false;
    }
} else {
    TM_INFO_LOG("g
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值