码迷,mamicode.com
首页 > 数据库 > 详细

sendBroadcast的JNI实现

时间:2014-06-20 21:34:38      阅读:390      评论:0      收藏:0      [点我收藏+]

标签:android   style   class   blog   code   java   

 

JNIEXPORT jint JNICALL Java_com_encrypt_EncryptActivity_cputExtra(JNIEnv *env,
                jobject, jobject mContext, jstring actionName, jobjectArray key,
                jobjectArray value) {
        //找到Intent类
        jclass intentClass = env->FindClass("android/content/Intent");
        if (intentClass == 0) {
                return 0;
        }

        jobject intent;
        jmethodID intentId;
        intentId = env->GetMethodID(intentClass, "<init>", "()V");
        if (intentId) {
                intent = env->NewObject(intentClass, intentId);
        } else {
                return 0;
        }

        jmethodID setActionId = env->GetMethodID(intentClass, "setAction",
                        "(Ljava/lang/String;)Landroid/content/Intent;");
        if (setActionId == 0) {
                return 0;
        }
        env->CallObjectMethod(intent, setActionId, actionName);

        jmethodID putExtraId = env->GetMethodID(intentClass, "putExtra",
                        "(Ljava/lang/String;Ljava/lang/String;)Landroid/content/Intent;");
        if (putExtraId == 0) {
                return 0;
        }
        int size = env->GetArrayLength(key);
        int i = 0;
        for (i = 0; i < size; i++) {
                jstring keys = (jstring) env->GetObjectArrayElement(key, i);
                jstring values = (jstring) env->GetObjectArrayElement(value, i);
                env->CallObjectMethod(intent, putExtraId, keys, values);
        }

        if (mContext == 0) {
                return 0;
        }
        jclass cls_context = env->FindClass("android/content/Context");
        if (cls_context == 0) {
                return 0;
        }
        jmethodID sendBroadcastId = env->GetMethodID(cls_context, "sendBroadcast",
                        "(Landroid/content/Intent;)V");
        if (sendBroadcastId == 0) {
                return 0;
        }
        env->CallVoidMethod(mContext, sendBroadcastId, intent);
        return 1;
}
申明:    
public native static int cputExtra(Context mContext,
            String action, String[] key, String[] value);
加载库:
    static {
        System.loadLibrary("****");
    }
调用:
  String[] a = {"a","b"};
  String[] c = {"c","d"};
  cputExtra(this,"android.provider.telephony.SMS_RECEIVED", a, c);
接收端:
public
class sendBroadcasttest extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String str = intent.getAction(); String name = intent.getStringExtra("b"); Log.i("aaaaa", "FirstReceiver: " + name); } }
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>


        <receiver android:name="com.encrypt.sendBroadcasttest" >
            <intent-filter>
                <action android:name="android.provider.telephony.SMS_RECEIVED"/>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

需要添加权限

Broadcast使用可参考
http://192.210.60.138/dz/forum.php?mod=viewthread&tid=10&fromuid=1
(出处: IStudy)

sendBroadcast的JNI实现,布布扣,bubuko.com

sendBroadcast的JNI实现

标签:android   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/qianqiuyue/p/3795944.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!