本文介绍了Android的 - 侦听传入的短信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过接收到的SMS创建用于监测接收短信的应用程序,并启动一个程序,也应该阅读短信内容。

工作流程:

短信发送到Android设备自可执行的应用程序阅读SMS信息

如果任何人能帮助我!

解决方案

 公共类SmsListener扩展的BroadcastReceiver {

    私人共享preferences preferences;

    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        // TODO自动生成方法存根

        如果(intent.getAction()。等于(android.provider.Telephony.SMS_RECEIVED)){
            捆绑包= intent.getExtras(); // ---得到通过短信---
            SmsMessage []封邮件= NULL;
            字符串msg_from;
            如果(捆绑!= NULL){
                // ---检索收到的短信---
                尝试{
                    [对象]的PDU =(对象[])bundle.get(的PDU);
                    封邮件=新SmsMessage [pdus.length]
                    的for(int i = 0; I< msgs.length;我++){
                        封邮件[I] = SmsMessage.createFromPdu((字节[])的PDU [I]);
                        msg_from =封邮件[I] .getOriginatingAddress();
                        字符串msgBody =封邮件[I] .getMessageBody();
                    }
                }赶上(例外五){
// Log.d(捕获到异常,e.getMessage());
                }
            }
        }
    }
}
 

请注意:在您的清单文件中添加的BroadcastReceiver -

 <接收机器人:名称=。listener.SmsListener>
    <意向滤光器>
        <作用机器人:名称=android.provider.Telephony.SMS_RECEIVED/>
    &所述; /意图滤光器>
< /接收器>
 

添加此权限:

 <使用-权限的Andr​​oid:名称=android.permission.RECEIVE_SMS/>
 

I am trying to create an application for monitoring incoming SMS messages, and launch a program via incoming SMS, also it should read the content from the SMS.

Workflow:

SMS sent to Android deviceself executable ApplicationRead the SMS information

If any one can help me!

解决方案
public class SmsListener extends BroadcastReceiver{

    private SharedPreferences preferences;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
            Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
            SmsMessage[] msgs = null;
            String msg_from;
            if (bundle != null){
                //---retrieve the SMS message received---
                try{
                    Object[] pdus = (Object[]) bundle.get("pdus");
                    msgs = new SmsMessage[pdus.length];
                    for(int i=0; i<msgs.length; i++){
                        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                        msg_from = msgs[i].getOriginatingAddress();
                        String msgBody = msgs[i].getMessageBody();
                    }
                }catch(Exception e){
//                            Log.d("Exception caught",e.getMessage());
                }
            }
        }
    }
}

Note: In your manifest file add the BroadcastReceiver-

<receiver android:name=".listener.SmsListener">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

Add this permission:

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

这篇关于Android的 - 侦听传入的短信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 00:31