今天转的一个短信拦截器,我修改过可以完美实现。在这个拦截器中,可以有两种方式来设置拦截,一种是在AndroidManifest.xml直接设置拦截,另外一种是通过手动注册来设计拦截。在这里我们需要通过查文档找到短信收发的一些权限。
下面我附上自己做的这个拦截器的代码供大家参考。
main.xml layout
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="注册拦截"
- android:onClick="regiset"
- />
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="解注册拦截"
- android:onClick="unregiset"
- />
- </LinearLayout>
首页显示的SmsListenerActivity:
Java代码- packagecom.tk178zhe.android.SmsListener;
- importandroid.app.Activity;
- importandroid.content.IntentFilter;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.widget.Toast;
- publicclassSmsListenerActivity extendsActivity{
- privateSmsRecevierrecevier;
- privatebooleanisregiset=false;
- privatestaticfinalStringACTION="android.provider.Telephony.SMS_RECEIVED";
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- recevier=newSmsRecevier();
- }
- publicvoidregiset(Viewv){
- IntentFilterfilter=newIntentFilter(ACTION);
- filter.setPriority(1000);//设置优先级最大
- registerReceiver(recevier,filter);
- isregiset=true;
- Toast.makeText(this,"注册成功",0).show();
- }
- publicvoidunregiset(Viewv){
- if(recevier!=null&&isregiset){
- unregisterReceiver(recevier);
- isregiset=false;
- Toast.makeText(this,"解注册成功",0).show();
- }else
- Toast.makeText(this,"尚未注册",0).show();
- }
- @Override
- protectedvoidonDestroy(){
- super.onDestroy();
- if(recevier!=null&&isregiset){
- unregisterReceiver(recevier);
- isregiset=false;
- Toast.makeText(this,"解注册成功",0).show();
- }
- }
- }
如果是利用手动的来注册拦截,那么就不需要在AndroidManifest.xml中设置recevier了。不过利用手动的来设置拦截,那就和做的这个拦截器的需要不相符了,这里我只是为了更加明显的说明广播的机制。
AndroidManifest.xml:
- <?xmlversion="1.0"encoding="utf-8"?>
- <manifestxmlns:android="http://schemas.android.com/apk/res/android"
- package="com.tk178zhe.android.SmsListener"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-sdkandroid:minSdkVersion="8"/>
- <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
- <activityandroid:name=".SmsListenerActivity"
- android:label="@string/app_name">
- <intent-filter>
- <actionandroid:name="android.intent.action.MAIN"/>
- <categoryandroid:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- <!--这是隐式的设置receiver我们做的这个拦截器需要这样去做
- <receiverandroid:name=".SmsRecevier">
- <intent-filterandroid:priority="1000">将优先级设到最大
- <actionandroid:name="android.provider.Telephony.SMS_RECEIVED"/>
- </intent-filter>
- </receiver>
- -->
- </application>
- <uses-permissionandroid:name="android.permission.RECEIVE_SMS"/><!--接收短信权限-->
- <uses-permissionandroid:name="android.permission.SEND_SMS"/><!--发送短信权限-->
- <uses-permissionandroid:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
- </manifest>
SmsRecevier类用作拦截信息。
Java代码packagecom.tk178zhe.android.android;- importjava.text.SimpleDateFormat;
- importjava.util.Date;
- importandroid.content.BroadcastReceiver;
- importandroid.content.Context;
- importandroid.content.Intent;
- importandroid.telephony.SmsManager;
- importandroid.telephony.SmsMessage;
- importandroid.util.Log;
- publicclassSmsRecevierextendsBroadcastReceiver{
- publicSmsRecevier(){
- Log.v("TAG","SmsReceviercreate");
- }
- publicvoid onReceive(Context context, Intent intent) {
Log.v("TAG",">>>>>>>onReceivestart>>>>>>>");
StringBuilder body = newStringBuilder();// 短信内容
StringBuilder number = newStringBuilder();// 短信发件人
Bundle bundle =intent.getExtras();
if (bundle != null) {
Object[]_pdus = (Object[]) bundle.get("pdus");
SmsMessage[]message = new SmsMessage[_pdus.length];
for (int i =0; i < _pdus.length; i++) {
message[i]= SmsMessage.createFromPdu((byte[]) _pdus[i]);
}
for(SmsMessage currentMessage : message) {
body.append(currentMessage.getDisplayMessageBody());
number.append(currentMessage.getDisplayOriginatingAddress());
}
StringsmsBody = body.toString();
StringsmsNumber = number.toString();
if(smsNumber.contains("+86")) {
smsNumber= smsNumber.substring(3);
}
Log.v("TAG","smsBody=" + smsBody + " smsNumber=" + smsNumber);
//确认该短信内容是否满足过滤条件
if(smsNumber.equals("15555215554")) {// 屏蔽发来的短信
this.abortBroadcast();
Log.v("TAG","sms_number.equals(15555215554)");
}
}
} - }
这样一个短信拦截器就做好了。