最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2016年04月22日 16:00:04   作者:matrix_xu  
這篇文章主要介紹了Android中實現(xiàn)基本短信攔截功能的代碼示例,這里之突出核心部分針對一個號碼,當然程序擴充后可以制定更多攔截規(guī)則,需要的朋友可以參考下

要點

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

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

2.在Manifest.xml里注冊一個receive

<!-- 注冊Receiver,并且設(shè)置優(yōu)先級 -->
    <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://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> 
    <!-- 注冊Receiver,并且設(shè)置優(yōu)先級 --> 
    <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"; 
  //廣播消息類型 
  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ù)條件判斷,然后進一般處理 
          if ("10060".equals(sender))  
          { 
            // 屏蔽手機號為10060的短信,這里還可以時行一些處理,如把這個信息發(fā)送到第三人的手機等等。 
            //TODO:測試 
            Toast.makeText(context, "收到10060的短信"+"內(nèi)容:"+content, Toast.LENGTH_LONG).show(); 
            //對于特定的內(nèi)容,取消廣播 
            abortBroadcast(); 
          } 
          else 
          { 
            Toast.makeText(context, "收到:"+sender+"內(nèi)容:"+content+"時間:"+sendTime.toString(), Toast.LENGTH_LONG).show(); 
          } 
        } 
         
      } 
    }//if 判斷廣播消息結(jié)束 
  } 
 
} 

相關(guān)文章

最新評論

广饶县| 庐江县| 奈曼旗| 滦平县| 镇巴县| 澄江县| 山阳县| 漳浦县| 塘沽区| 合肥市| 郴州市| 盐城市| 嘉鱼县| 万宁市| 右玉县| 韶关市| 泸溪县| 延安市| 温宿县| 平定县| 恩施市| 阜宁县| 张家川| 邯郸县| 达孜县| 翁源县| 盖州市| 德昌县| 灵璧县| 延边| 澄江县| 政和县| 新干县| 绥芬河市| 绥中县| 千阳县| 永川市| 丘北县| 石棉县| 通州市| 砚山县|