Android怎么拦截固定内容短信(求几行代码)

移动开发 码拜 8年前 (2016-09-18) 975次浏览
Android短信拦截,求几行代码,拦截固定内容,例如短信开头三个字母为“KEY”的短信
解决方案

35

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.sax.StartElementListener;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.text.TextUtils;
import android.util.Log;
public class SMSreceiver extends BroadcastReceiver {

	private static final String TAG = "SMSReceiver";
	@Override
	public void onReceive(Context context, Intent intent) {
		// 写接收短信的代码
		Object[] objs = (Object[]) intent.getExtras().get("pdus");

		for(Object b:objs){
			//具体的某一条短信
			SmsMessage sms =SmsMessage.createFromPdu((byte[]) b);
			//发送者
			String sender = sms.getOriginatingAddress();//15555555556


			Log.i(TAG, "====sender=="+sender);
			String body = sms.getMessageBody();
                         //body是短信内容
			if (body.startsWith("KEY")) {
				abortBroadcast();
			}
 
	}
	}}

这个写完需要在manifest文件中配置一下
<intent-filter  android:priority=”1000″>
<action android:name=”android.provider.Telephony.SMS_RECEIVED”/>
</intent-filter>,然后记得给权限

5

简单的广播接收是4.4版本之前的方法,4.4之后已经不适用了,4.4之后对短信的权限有限制,百度或Google一下都有很多资料的

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Android怎么拦截固定内容短信(求几行代码)
喜欢 (0)
[1034331897@qq.com]
分享 (0)