首页 > 代码库 > android 拦截短信

android 拦截短信

SMSReceiver.java

 1 package com.gbg.mybroadcastreceiver; 2  3 import android.content.BroadcastReceiver; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.os.Bundle; 7 import android.telephony.SmsMessage; 8 import android.util.Log; 9 import android.widget.Toast;10 11 public class SMSReceiver extends BroadcastReceiver {12 13     @Override14     public void onReceive(Context context, Intent intent) {15         Log.i("app", "shoudao");16         Bundle bundle = intent.getExtras();17         Object messages[] = (Object[])bundle.get("pdus");18         SmsMessage smsMessage[] = new SmsMessage[messages.length];19         for (int i = 0; i < smsMessage.length; i++) {20             smsMessage[i] = SmsMessage.createFromPdu((byte[])messages[i]);21         }22         Toast.makeText(context, "您有"+messages.length+"条短信。。。", 1).show();;23     }24 25 }
View Code

 

 

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.gbg.mybroadcastreceiver"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="21" />    <uses-permission android:name="android.permission.RECEIVE_SMS"/>    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity android:name=".MainActivity">            <intent-filter >                <action android:name="android.intent.action.MAIN"/>                <category android:name="android.intent.category.LAUNCHER"/>            </intent-filter>        </activity>        <service android:name=".LocalService" android:process="com.gbg.mybroadcastreceiver.localservice"  android:enabled="true">            <intent-filter android:priority="1000">                <category android:name="com.gbg.localservice"/>            </intent-filter>        </service>        <receiver android:name="com.gbg.mybroadcastreceiver.SMSReceiver" android:enabled="true">            <intent-filter android:priority="999">                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>                <category android:name="android.intent.category.DEFAULT"/>            </intent-filter>        </receiver>    </application></manifest>
View Code

 

android 拦截短信