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)