国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android中實(shí)現(xiàn)基本的短信攔截功能的代碼示例

2019-12-12 06:31:43
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

要點(diǎn)

1.在Manifest.xml里加"接收"SMS的權(quán)限

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

2.在Manifest.xml里注冊(cè)一個(gè)receive

<!-- 注冊(cè)Receiver,并且設(shè)置優(yōu)先級(jí) -->    <receiver android:name=".AutoSMS" android:exported="false">  <intent-filter android:priority="1000">  <action android:name="android.provider.Telephony.SMS_RECEIVED"/>  </intent-filter> </receiver>

3.定義一個(gè)短信接收類(lèi),并且重寫(xiě)onReceive

//繼承BroadcastReceiverpublic class AutoSMS extends BroadcastReceiver { private String TAG="AutSMS"; //廣播消息類(lèi)型 public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED"; //覆蓋onReceive方法 @Override public void onReceive(Context context, Intent intent)  {.....

實(shí)例
下面是完整的代碼:

Manifest.xml:

<manifest xmlns:android="http://schemas.android.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>     <!-- 注冊(cè)Receiver,并且設(shè)置優(yōu)先級(jí) -->     <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> 

AutoSMS.java:

package com.xxh.autosms;  import java.text.SimpleDateFormat; import java.util.Date;  import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.telephony.SmsMessage; import android.util.Log; import android.widget.Toast;  //繼承BroadcastReceiver public class AutoSMS extends BroadcastReceiver  {    private String TAG="AutSMS";   //廣播消息類(lèi)型   public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";   //覆蓋onReceive方法   @Override   public void onReceive(Context context, Intent intent)    {     // TODO Auto-generated method stub     Log.i(TAG, "引發(fā)接收事件");     //StringBuilder body=new StringBuilder("");//短信內(nèi)容     //StringBuilder sender=new StringBuilder("");//發(fā)件人     //先判斷廣播消息     String action = intent.getAction();     if (SMS_RECEIVED_ACTION.equals(action))     {       //獲取intent參數(shù)       Bundle bundle=intent.getExtras();       //判斷bundle內(nèi)容       if (bundle!=null)       {         //取pdus內(nèi)容,轉(zhuǎn)換為Object[]         Object[] pdus=(Object[])bundle.get("pdus");         //解析短信         SmsMessage[] messages = new SmsMessage[pdus.length];         for(int i=0;i<messages.length;i++)         {           byte[] pdu=(byte[])pdus[i];           messages[i]=SmsMessage.createFromPdu(pdu);         }           //解析完內(nèi)容后分析具體參數(shù)         for(SmsMessage msg:messages)         {           //獲取短信內(nèi)容           String content=msg.getMessageBody();           String sender=msg.getOriginatingAddress();           Date date = new Date(msg.getTimestampMillis());           SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");           String sendTime = sdf.format(date);           //TODO:根據(jù)條件判斷,然后進(jìn)一般處理           if ("10060".equals(sender))            {             // 屏蔽手機(jī)號(hào)為10060的短信,這里還可以時(shí)行一些處理,如把這個(gè)信息發(fā)送到第三人的手機(jī)等等。             //TODO:測(cè)試             Toast.makeText(context, "收到10060的短信"+"內(nèi)容:"+content, Toast.LENGTH_LONG).show();             //對(duì)于特定的內(nèi)容,取消廣播             abortBroadcast();           }           else           {             Toast.makeText(context, "收到:"+sender+"內(nèi)容:"+content+"時(shí)間:"+sendTime.toString(), Toast.LENGTH_LONG).show();           }         }                }     }//if 判斷廣播消息結(jié)束   }  } 

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 泰州市| 大悟县| 合肥市| 南昌县| 沙湾县| 大竹县| 桃园市| 普宁市| 衡东县| 彭泽县| 贡嘎县| 吉水县| 郴州市| 漳州市| 锦州市| 波密县| 珲春市| 科技| 无棣县| 雷波县| 中牟县| 同江市| 赫章县| 河西区| 三原县| 连云港市| 梁平县| 浮山县| 安图县| 灵寿县| 商都县| 鹤岗市| 沈丘县| 龙南县| 东乡县| 邓州市| 康保县| 祥云县| 夏津县| 城固县| 盖州市|