zoukankan      html  css  js  c++  java
  • Android之——拦截短信

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46994097
    

    这里。向大家简介通过BroadcastReceiver来拦截短信的方法

    1、创建短信广播接收者SmsRecevier

    这个类是BroadcastReceiver的子类,详细的拦截操作在这个类中实现。我在这里仅仅是简单的介绍一下方法,把获取到的短信打印信息出来。

    详细的业务逻辑就要大家自己去实现了。

    详细代码例如以下:

    package com.lyz.receiver;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.telephony.SmsMessage;
    import android.util.Log;
    
    /**
     * 短信广播接收者
     * @author liuyazhuang
     *
     */
    public class SmsRecevier extends BroadcastReceiver {
    	private static final String TAG = "SmsRecevier";
    	@Override
    	public void onReceive(Context context, Intent intent) {
    		Object[] pdus = (Object[]) intent.getExtras().get("pdus");
    		for(Object pdu:pdus){
    			SmsMessage smsMessage = SmsMessage.createFromPdu((byte[])pdu);
    			String body = smsMessage.getDisplayMessageBody();
    			Log.i(TAG, body);
    		}
    	}
    } 

    2、注冊授权信息

    详细实现例如以下:

    <?

    xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.lyz.sms" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.RECEIVE_SMS"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.lyz.sms.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- 配置广播拦截短信 --> <receiver android:name="com.lyz.receiver.SmsRecevier"> <intent-filter android:priority="1000"> <action android:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver> </application> </manifest>

    大功告成。是不是和《Android之——拦截外拨电话》一样简单呢?
    温馨提示:大家能够到http://download.csdn.net/detail/l1028386804/8921125拦截获取完整的代码演示样例

  • 相关阅读:
    swoole 入门
    Centos7安装Percona5.7
    clone github报Permission denied (publickey) 解决方案
    yii2-swiftmailer入门
    Yii 2.0 数据库操作总结
    面向对象简单示例
    面向对象与面向过程
    Tkinter之部件3种放置方式pack、grid、place
    Tkinter之variable用法
    Tkinter之Menu
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/7010647.html
Copyright © 2011-2022 走看看