在Android開(kāi)發(fā)中,實(shí)現(xiàn)短信攔截功能可以是很基礎(chǔ)的,今天武林技術(shù)頻道小編想記錄這方面的知識(shí),希望本文能為大家?guī)?lái)更多有用的學(xué)習(xí)內(nèi)容。
要點(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é)束 } } 以上就是武林技術(shù)頻道小編介紹的Android中實(shí)現(xiàn)基本的短信攔截功能的代碼示例,如果你已經(jīng)學(xué)會(huì)這些操作方法,那么趕快去操作試試吧!
新聞熱點(diǎn)
疑難解答
圖片精選