首页 > 代码库 > sendBroadcast的JNI实现
sendBroadcast的JNI实现
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)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。