您的位置:首页 > 移动开发 > Android开发

浅谈Android平台短信拦截的几种实现方式(包括NDK注册短信监听器)

2014-11-16 16:30 459 查看
       移动端安全形势日益严峻。特别是在android平台,很多恶意软件在后台悄无声息的向运营商发送订阅服务短信,窃取用户资费。他们都对用户接收到的短信进行了拦截。下面是我总结的4中常用拦截方式。

Android短信拦截,总的来说可以分成两大类:

1. 监听短信数据库的变化

在代码中,实现注册短信监听类,监听短信数据库的变换,把指定号码或指定内容的短信屏蔽掉,这种方式是一种“假”方式,其实是在收件箱收到短信之后,再删除指定的短信。

1).申请短信读写权限

<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>


2).注册短信内容观察回调类:

SmsContent content = new SmsContent(new Handler());
this.getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, content);


3).实现回调类:

public class SmsContent extends ContentObserver{
public void onChange(boolean selfChange) {
// TODO Auto-generated method stub
super.onChange(selfChange);
Log.i("contentmessage", "onChange!");
// 在这里你可以对短信数据库进行任意操作
}


2. 注册短信广播监听器

       注册短信广播监听器方式又分为静态注册和动态注册。动态注册又可以细分为Java层注册和Native层注册。

(1). 静态注册:

1)权限声明:在AndroidManifest.xml中声明权限:

<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>


2)设置接收短信广播的Receiver及其优先级:

<receiver android:name = "com.example.messagetest.SmsReceiver">
<intent-filter android:priority="2324324234">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>


3)实现Receiver

public class SmsReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
//在这里可以对接收到的短信进行想要的处理
}
}


(2). 动态注册—Java层:

1)权限声明:在AndroidManifest.xml中声明权限:

<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>


2)实现Receiver

public class SmsReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
//在这里可以对接收到的短信进行想要的处理
}
}


3)动态注册Receiver:

IntentFilter iFilter = null;
mReceiver = new Receiver();
iFilter = new
IntentFilter("android.provider.Telephony.SMS_RECEIVED");
iFilter.setPriority(Integer.MAX_VALUE);
registerReceiver(mReceiver, iFilter);


(3). 动态注册—native层:

1.权限声明:在AndroidManifest.xml中声明权限:

<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>


2.实现Receiver响应类:

public class Receiver extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
//在这里可以对接收到的短信进行想要的处理
}
}


3). Native层注册Receiver:

/*生成一般实例对象*/
jobject getInstance(JNIEnv* env, jclass obj_class) {
jmethodID construction_id = (*env) -> GetMethodID(env, obj_class, "<init>", "()V");
jobject obj = (*env) -> NewObject(env, obj_class, construction_id);
return obj;
}
/*生成IntentFilter实例对象*/
jobject getIntentFilterInstance(JNIEnv* env, jclass obj_class, jstring
stringparam) {
jmethodID construction_id = (*env) -> GetMethodID(env, obj_class, "<init>", "
(Ljava/lang/String;)V");
jobject obj = (*env) -> NewObject(env, obj_class, construction_id, stringparam);
return obj;
}
/*动态注册实现函数,注意加粗部分*/
JNIEXPORT void JNICALL
Java_com_example_nativeregistermessage_MainActivity_RegisterReceiver
(JNIEnv *env, jobject obj) {
/*生成IntentFilter对象*/
j
4000
class intentfilter = (*env) -> FindClass(env, "android/content/IntentFilter");
if (0 == intentfilter) return;
char* tempchar = "android.provider.Telephony.SMS_RECEIVED";
jobject intentfilter_obj = getIntentFilterInstance(env, intentfilter, (*env)
-> NewStringUTF(env, tempchar));
if (0 == intentfilter_obj) return;
/*调用IntentFilter的setPriority函数来设置Receiver的优先级*/
jmethodID mintentfilter = (*env) -> GetMethodID(env, intentfilter, "setPriority","(I)V");//
if (0 == mintentfilter) return;
(*env) -> CallVoidMethod(env, intentfilter_obj, mintentfilter, (jint)1000);
/*生成短信广播监听器的回调对象*/
jclass receiverclass = (*env)
-> FindClass(env, "com/example/nativeregistermessage/Receiver");
jobject receiver_obj = getInstance(env, receiverclass);
/*调用当前context中的registerreceiver来注册短信广播监听器*/
jclass registerreceiver = (*env) -> FindClass(env, "android/content/ContextWrapper");
jmethodID mregisterreceiver = (*env)
-> GetMethodID(env, registerreceiver, "registerReceiver","
(Landroid/content/BroadcastReceiver;Landroid/content/IntentFilter;)
Landroid/content/Intent;");
if (0 == mregisterreceiver) return;
(*env) -> CallObjectMethod(env, obj, mregisterreceiver, receiver_obj, intentfilter_obj);
}


Native实现短信拦截Demo:http://download.csdn.net/detail/hookfeng/8164315

以上是我已知的短信拦截方式,如还有其它实现方式,请赐教,谢谢。

以上提到的技术仅限技术交流使用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: