Android中实现基本的短信拦截功能的代码示例

2019-12-10 18:14:43丽君
易采站长站为您分析Android中实现基本短信拦截功能的代码示例,这里之突出核心部分针对一个号码,当然程序扩充后可以制定更多拦截规则,需要的朋友可以参考下  

要点

1.在Manifest.xml里加"接收"SMS的权限

<uses-permission Android:name="android.permission.RECEIVE_SMS"></uses-permission>

2.在Manifest.xml里注册一个receive

<!-- 注册Receiver,并且设置优先级 -->
    <receiver android:name=".AutoSMS" android:exported="false">
  <intent-filter android:priority="1000">
  <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
  </intent-filter>
 </receiver>

3.定义一个短信接收类,并且重写onReceive

//继承BroadcastReceiver
public class AutoSMS extends BroadcastReceiver 
{

 private String TAG="AutSMS";
 //广播消息类型
 public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";
 //覆盖onReceive方法
 @Override
 public void onReceive(Context context, Intent intent) 
 {
.....

实例
下面是完整的代码:

Manifest.xml:

<manifest xmlns:android="http://www.easck.com/apk/res/android" 
  package="com.xxh.autosms" 
  android:versionCode="1" 
  android:versionName="1.0" > 
 
  <uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="15" /> 
  <uses-permission android:name="android.permission.RECEIVE_SMS"/> 
 
  <application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
      android:name=".MainActivity" 
      android:label="@string/title_activity_main" > 
      <intent-filter> 
        <action android:name="android.intent.action.MAIN" /> 
 
        <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
    </activity> 
    <!-- 注册Receiver,并且设置优先级 --> 
    <receiver android:name=".AutoSMS" android:exported="false"> 
      <intent-filter android:priority="1000"> 
        <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 
      </intent-filter> 
    </receiver> 
     
  </application> 
 
</manifest>