接收短信的Andr​​oid应用程序 [英] Receiving SMS on Android App

查看:234
本文介绍了接收短信的Andr​​oid应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也跟着教程收到短信我的应用程序,并阅读到SMSbody传递给面包。 那就是接收器类。

i followed a tutorial to receive SMS on my application and read it to pass the SMSbody to Toast. that is the Receiver class.

public class SmsReciever extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent){
        Bundle bundle= intent.getExtras();
        SmsMessage[] msgs= null;
        String str="";
        if(bundle != null ){
            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]);
                str+= msgs[i].getMessageBody();

            }
             Toast.makeText(context, str, Toast.LENGTH_LONG).show();
        }
        }

    }

清单文件

    <receiver android:name="com.msoft.masrooq.SmsReciever">
    <intent-filter>
    <action android:name="android.provider.telephony.SMS_RECIEVED"></action>
    </intent-filter>
    </receiver>
        <uses-permission android:name="android.permission.RECEIVE_SMS"/>
            <uses-permission android:name="android.permission.READ_SMS" />

应用程序启动正常,但它不会响应接收短信 它没有做任何事情。

the app starts fine but it doesn't response to receiving sms it doesn't do anything.

推荐答案

下面是我实现接收短信的。短信可分为多种,注意如何治疗。同时检查机器人:优先级属性

Here is my implementation of receiving sms messages. Sms message may be broken into many, notice how it is treated. Also check the android:priority attribute.

public class SmsReceiver extends BroadcastReceiver {

    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(SMS_RECEIVED)) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                // get sms objects
                Object[] pdus = (Object[]) bundle.get("pdus");
                if (pdus.length == 0) {
                    return;
                }
                // large message might be broken into many
                SmsMessage[] messages = new SmsMessage[pdus.length];
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < pdus.length; i++) {
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                    sb.append(messages[i].getMessageBody());
                }
                String sender = messages[0].getOriginatingAddress();
                String message = sb.toString();
                Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
                // prevent any other broadcast receivers from receiving broadcast
                // abortBroadcast();
            }
        }
    }
}

AndroidManifest.xml中

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.smsreceiver"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="4" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity
            android:name=".SmsLoggerActivity"
            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.example.smsreceiver.SmsReceiver" android:enabled="true">
            <intent-filter android:priority="2147483647">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

几点注意事项: 如果你声明的接收器XML不是系统可以使用接收器,无论您的应用程序被不断推出。 由于有关收到的短信Android 1.6的通知交付订购的节目,你可以使用安卓&LT优先属性;意向滤光器&gt; 来告诉系统首先发送短信到你的应用程序(也可以调用abortBroadcast()这样的其他应用程序将不会收到短信,如本地短信应用程序)。不要忘了广播接收器具有执行其操作约10秒钟,否则可以完成自己的工作之前,prematurely终止。

Few notes: If you declare your receiver in xml than system can use your receiver regardless of your application was ever launched. Since Android 1.6 notifications about received sms messages are delivered as ordered broadcasts, you can use android:priority attribute of <intent-filter> to tell the system send the sms first to your application (you can also call abortBroadcast() so other applications won't receive the sms, e.g. the native sms app). Don't forget broadcast receiver has about 10 seconds for executing its operation, otherwise it can be prematurely terminated before finishing its job.

这篇关于接收短信的Andr​​oid应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆