发送和接收短信和彩信的Andr​​oid(pre奇巧的Andr​​oid 4.4) [英] Sending and Receiving SMS and MMS in Android (pre Kit Kat Android 4.4)

查看:187
本文介绍了发送和接收短信和彩信的Andr​​oid(pre奇巧的Andr​​oid 4.4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了如何发送和接收的短信的消息。要发送的短信的消息我只好打电话给 sendTextMessage() sendMultipartTextMessage()方法在 SmsManager的的类。要接收的短信的消息,我必须注册在 AndroidMainfest.xml 文件的接收器。然后,我不得不重写的BroadcastReceiver 的onReceive()方法。我已经包括下面的例子。

MainActivity.java

 公共类MainActivity延伸活动{
    私有静态字符串SENT =SMS_SENT;
    私有静态字符串DELIVERED =SMS_DELIVERED;
    私有静态诠释MAX_SMS_MESSAGE_LENGTH = 160;

    // ---发送SMS消息到另一装置---
    公共静态无效sendSMS(字符串phoneNumber的,字符串消息){

        PendingIntent piSent = PendingIntent.getBroadcast(mContext,0,新意图(SENT),0);
        PendingIntent piDelivered = PendingIntent.getBroadcast(mContext,0,新的意向(送到),0);
        SmsManager的SmsManager的= SmsManager.getDefault();

        INT长度= message.length();
        如果(长度> MAX_SMS_MESSAGE_LENGTH){
            ArrayList的<字符串>将MessageList = smsManager.divideMessage(消息);
            smsManager.sendMultipartTextMessage(phoneNumber的,空,将MessageList,NULL,NULL);
        }
        其他
            smsManager.sendTextMessage(phoneNumber的,空,消息,piSent,piDelivered);
        }
    }

    // MainActivity更多方法...
}
 

SMSReceiver.java

 公共类SMSReceiver扩展的BroadcastReceiver {
    私人最终字符串DEBUG_TAG =的getClass()getSimpleName()的toString()。
    私有静态最后弦乐ACTION_SMS_RECEIVED =android.provider.Telephony.SMS_RECEIVED;
    私人语境mContext;
    私人意图mIntent;

    //获得短信
    公共无效的onReceive(上下文的背景下,意图意图){
        mContext =背景;
        mIntent =意图;

        串动= intent.getAction();

        如果(action.equals(ACTION_SMS_RECEIVED)){

            字符串的地址,海峡=;
            INT的ContactID = -1;

            SmsMessage []封邮件= getMessagesFromIntent(mIntent);
            如果(消息!= NULL){
                的for(int i = 0; I< msgs.length;我++){
                    地址=封邮件[I] .getOriginatingAddress();
                    使用ContactID = ContactsUtils.getContactId(mContext,地址,地址);
                    STR + =封邮件[I] .getMessageBody()的toString()。
                    STR + =\ N的;
                }
            }

            如果(的ContactID!= -1){
                showNotification(的ContactID,STR);
            }

            // ---发送广播意图来更新在接收的短信
            // 活动 - -
            意图broadcastIntent =新意图();
            broadcastIntent.setAction(SMS_RECEIVED_ACTION);
            broadcastIntent.putExtra(SMS,STR);
            context.sendBroadcast(broadcastIntent);
        }

    }

    公共静态SmsMessage [] getMessagesFromIntent(意向意图){
        [对象]消息=(对象[])intent.getSerializableExtra(的PDU);
        字节[] [] pduObjs =新的字节[messages.length] [];

        的for(int i = 0; I< messages.length;我++){
            pduObjs [I] =(byte []的)消息[I]
        }
        字节[] []的PDU =新的字节[pduObjs.length] [];
        INT pduCount = pdus.length;
        SmsMessage []封邮件=新SmsMessage [pduCount]
        的for(int i = 0; I< pduCount;我++){
            PDU的[I] = pduObjs [I]
            封邮件[I] = SmsMessage.createFromPdu(PDU的[I]);
        }
        返回封邮件;
    }

    / **
    *该通知是在状态的图标和相关扩展进入
    * 酒吧。
    * /
    保护无效showNotification(INT的ContactID,字符串消息){
        //显示通知...
    }
}
 

AndroidManifest.xml中

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.myexample
    安卓版code =1
    机器人:VERSIONNAME =1.0>

    <用途-SDK
        安卓的minSdkVersion =16
        机器人:targetSdkVersion =17/>

    <使用-权限的Andr​​oid:名称=android.permission.READ_CONTACTS/>
    <使用-权限的Andr​​oid:名称=android.permission.READ_PHONE_STATE/>
    <使用-权限的Andr​​oid:名称=android.permission.SEND_SMS/>
    <使用-权限的Andr​​oid:名称=android.permission.RECEIVE_SMS/>
    <使用-权限的Andr​​oid:名称=android.permission.READ_SMS/>
    <使用-权限的Andr​​oid:名称=android.permission.WRITE_SMS/>
    <使用-权限的Andr​​oid:名称=android.permission.RECEIVE_MMS/>
    <使用-权限的Andr​​oid:名称=android.permission.WRITE/>
    <使用-权限的Andr​​oid:名称=android.permission.VIBRATE/>
    <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
    <使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>

    <应用
        机器人:可调试=真
        机器人:图标=@可绘制/ ic_launcher_icon
        机器人:标签=@字符串/ APP_NAME>

        <活动
            //主要业务...
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>

                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动
            //活动2 ...
        < /活性GT;
        //更多acitivies ...

        //短信接收器
        <接收器的Andr​​oid版本:NAME =com.myexample.receivers.SMSReceiver>
            <意向滤光器>
                <作用机器人:名称=android.provider.Telephony.SMS_RECEIVED/>
            &所述; /意图滤光器>
        < /接收器>

    < /用途>
< /舱单>
 

不过,我想知道,如果你可以发送和接收以类似的方式彩信的消息。之后做一些研究,提供了关于博客的许多例子简单地传递一个意图本机消息应用程序。我试图发送一个彩信不留我的应用程序。似乎没有被发送和接收的彩信的标准方法。有没有人得到这个工作?

另外,我知道,短信/彩信的ContentProvider是不是官方 Android SDK中的一部分,但我想有人会已经能够实现这一点。任何帮助是极大的AP preciated。

更新

我添加了一个的BroadcastReceiver 的Andr​​oidManifest.xml 文件接收彩信消息

 <接收器的Andr​​oid版本:NAME =com.sendit.receivers.MMSReceiver>
    <意向滤光器>
        <作用机器人:名称=android.provider.Telephony.WAP_PUSH_RECEIVED/>

        <数据机器人:MIMETYPE =应用/ vnd.wap.mms消息/>
    &所述; /意图滤光器>
< /接收器>
 

在MMSReceiver类中,的onReceive()方法只能够抓住该信息被发送的phoneNumber的。你怎么抓其他重要的东西从彩信如文件路径媒体附件(图片/音频/视频),或在彩信

MMSReceiver.java

 公共类MMSReceiver扩展的BroadcastReceiver {
    私人最终字符串DEBUG_TAG =的getClass()getSimpleName()的toString()。
    私有静态最后弦乐ACTION_MMS_RECEIVED =android.provider.Telephony.WAP_PUSH_RECEIVED;
    私有静态最后弦乐MMS_DATA_TYPE =应用/ vnd.wap.mms消息;

     //获得MMS
    公共无效的onReceive(上下文的背景下,意图意图){

        串动= intent.getAction();
        字符串类型= intent.getType();

        如果(action.equals(ACTION_MMS_RECEIVED)及&安培; type.equals(MMS_DATA_TYPE)){

            捆绑包= intent.getExtras();

            Log.d(DEBUG_TAG,捆绑+包);
            SmsMessage []封邮件= NULL;
            字符串str =;
            INT的ContactID = -1;
            字符串的地址;

            如果(捆绑!= NULL){

                byte []的缓冲区= bundle.getByteArray(数据);
                Log.d(DEBUG_TAG,缓冲+缓冲);
                字符串incomingNumber =新的String(缓冲区);
                INT INDX = incomingNumber.indexOf(/ TYPE);
                如果(INDX大于0&安培;及(INDX-15)大于0){
                    INT newIndx = INDX  -  15;
                    incomingNumber = incomingNumber.substring(newIndx,INDX);
                    INDX = incomingNumber.indexOf(+);
                    如果(INDX大于0){
                        incomingNumber = incomingNumber.substring(INDX);
                        Log.d(DEBUG_TAG,手机号码:+ incomingNumber);
                    }
                }

                INT TRANSACTIONID = bundle.getInt(TRANSACTIONID);
                Log.d(DEBUG_TAG,TRANSACTIONID+ TRANSACTIONID);

                INT pduType = bundle.getInt(pduType);
                Log.d(DEBUG_TAG,pduType+ pduType);

                byte []的缓冲器2 = bundle.getByteArray(头);
                字符串标题=新的String(缓冲器2);
                Log.d(DEBUG_TAG,头+头);

                如果(的ContactID!= -1){
                    showNotification(的ContactID,STR);
                }

                // ---发送广播意图来更新MMS接收在
                // 活动 - -
                意图broadcastIntent =新意图();
                broadcastIntent.setAction(MMS_RECEIVED_ACTION);
                broadcastIntent.putExtra(彩信,STR);
                context.sendBroadcast(broadcastIntent);

            }
        }

    }

    / **
    *该通知是在状态的图标和相关扩展进入
    * 酒吧。
    * /
    保护无效showNotification(INT的ContactID,字符串消息){
        //显示通知...
    }
}
 

根据<一href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.1.1_r1/android/provider/Telephony.java?av=f">Documentation android.provider.Telephony 的:

广播动作:一个新的基于文本的短信已收到由设备。这样做的目的将具有以下额外的值:

的PDU - 一个Object [] OD的byte []包含的PDU s表示组成信息

额外的值可以使用getMessagesFromIntent(android.content.Intent)中提取。 如果一个BroadcastReceiver遇到一个错误,而处理这个意图应该设置适当的结果code。

  @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
公共静态最后弦乐SMS_RECEIVED_ACTION =android.provider.Telephony.SMS_RECEIVED;
 

广播动作:一个基于新的数据短信已收到由设备。这样做的目的将具有以下额外的值:

的PDU - 一个Object []字节[]包含的PDU s表示组成信息

额外的值可以使用getMessagesFromIntent(android.content.Intent)中提取。 如果一个BroadcastReceiver遇到一个错误,而处理这个意图应该设置适当的结果code。

  @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
公共静态最后弦乐DATA_SMS_RECEIVED_ACTION =android.intent.action.DATA_SMS_RECEIVED;
 

广播动作:一个新的WAP PUSH消息已被接收的装置。这样做的目的将具有以下额外的值:

TRANSACTIONID(整数) - 的WAP事务ID

pduType(整数) - 的WAP PDU类型

头(字节[]) - 消息的标题

数据(字节[]) - 消息的数据有效载荷

contentTypeParameters(HashMap的&LT;字符串,字符串&GT;) - 与内容类型相关联的任何参数(从WSP Content-Type头去codeD)

如果一个BroadcastReceiver遇到一个错误,而处理这个意图应该设置适当的结果code。 该contentTypeParameters额外的价值是地图由他们的名字键入内容的参数。 如果有任何未分配的著名参数遇到,地图的关键是未分配/ 0X ......',其中'...'是未指定参数的十六进制值。如果一个参数没有-价值在地图中的值为null。

  @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
公共静态最后弦乐WAP_PUSH_RECEIVED_ACTION =android.provider.Telephony.WAP_PUSH_RECEIVED;
 

更新#2

我已经找到了如何通过额外的 PendingIntent 被接收的的BroadcastReceiver : <一href="http://stackoverflow.com/questions/14571564/android-pendingintent-extras-not-received-by-broadcastreceiver">Android PendingIntent演员,不是靠的BroadcastReceiver

不过,多余的被传递到的 SendBroadcastReceiver 的不是的 SMSReceiver 的。如何传递一个额外的 SMSReceiver 的?

更新#3

接收彩信

所以,做更多的研究后,我看到了登记 ContentObserver 的几点建议。这样,您就可以检测到有任何改变内容://彩信,短信/通话内容提供商,从而让您能够探测到来袭彩信。这里是最接近的例子来得到这个工作,我发现:<一href="http://www.anddev.org/other-coding-problems-f5/where-can-i-find-information-about-sending-receiving-mms-t51284.html">Receiving MMS

不过,有一个变量 mainActivity 类型的ServiceController 。哪里的ServiceController 类实现的?是否有任何其他实现注册 ContentObserver

发送MMS

至于发送彩信,我也碰到过这样的例子:发送彩信

现在的问题是,我试图跑在我的Nexus 4,这是在Android v4.2.2这个code和我收到此错误:

  java.lang.SecurityException异常:没有写权限APN设置:无论是用户10099也不是当前进程android.permission.WRITE_APN_SETTINGS。
 

错误查询运营商的ContentProvider在 getMMSApns()的<$ C $的方法后,被扔C> APNHelper 类。

 最后光标apnCursor = this.context.getContentResolver()查询(Uri.withAppendedPath(Carriers.CONTENT_URI,当前),NULL,NULL,NULL,NULL);
 

显然,你不能阅读的APN在安卓4.2

什么是替代所有使用移动数据执行操作(如发送MMS),不知道默认APN设置present设备中的那些应用程序?

更新#4

发送MMS

我已经试过下面这个例子:发送彩信

由于@Sam在他的回答提示:

你要jsoup添加到构建路径,罐子到构建路径和进口com.droidprism *。要做到这一点在Android中,罐子首先添加到库目录下,然后配置项目的构建路径中的库目录已经在使用的罐子,然后构建路径配置点击订单和出口,并检查罐子和移动的箱子jsoup和droidprism罐子到构建顺序的顶部。

所以,现在我不再让SecurityException异常错误。我对Android的奇巧一台Nexus 5目前正在测试。运行示例code它给我的号召后,200响应code后

  MMResponse mmResponse = sender.send(满分,isProxySet,MMSProxy,MMSPort);
 

不过,我检查了我试着发送彩信的人。他们说,他们从来没有收到彩信。

解决方案

我拥有了你上面描述(Galaxy Nexus的对T-Mobile美国)完全相同的问题,这是因为移动数据是关闭的。

在果冻豆是: 设置>数据使用>移动数据

请注意,我必须有移动数据打开之前,发送彩信或接收之一。如果我收到一条彩信关闭移动数据,我会得到一个新的消息的通知,我会收到消息有一个下载按钮。但是,如果我没有事先移动数据,传入的彩信附件将不被接受。即使我把它在收到消息后。

由于某些原因,当你的手机供应商使您能够收发彩信,你必须启用移动数据,即使你使用的是无线网络,如果启用了移动数据,您将能够接收和发送彩信的能力即使WiFi是显示为您的互联网设备上。

这是一个真正的痛苦,因为如果你没有它,该消息可以在移动数据转弯时挂了很多,甚至,并且可能需要设备的重启。

I have figured out how to send and receive SMS messages. To send SMS messages I had to call the sendTextMessage() and sendMultipartTextMessage() methods of the SmsManager class. To receive SMS messages, I had to register a receiver in the AndroidMainfest.xml file. Then I had to override the onReceive() method of the BroadcastReceiver. I have included examples below.

MainActivity.java

public class MainActivity extends Activity {
    private static String SENT = "SMS_SENT";
    private static String DELIVERED = "SMS_DELIVERED";
    private static int MAX_SMS_MESSAGE_LENGTH = 160;

    // ---sends an SMS message to another device---
    public static void sendSMS(String phoneNumber, String message) {

        PendingIntent piSent = PendingIntent.getBroadcast(mContext, 0, new Intent(SENT), 0);
        PendingIntent piDelivered = PendingIntent.getBroadcast(mContext, 0,new Intent(DELIVERED), 0);
        SmsManager smsManager = SmsManager.getDefault();

        int length = message.length();          
        if(length > MAX_SMS_MESSAGE_LENGTH) {
            ArrayList<String> messagelist = smsManager.divideMessage(message);          
            smsManager.sendMultipartTextMessage(phoneNumber, null, messagelist, null, null);
        }
        else
            smsManager.sendTextMessage(phoneNumber, null, message, piSent, piDelivered);
        }
    }

    //More methods of MainActivity ...
}

SMSReceiver.java

public class SMSReceiver extends BroadcastReceiver {
    private final String DEBUG_TAG = getClass().getSimpleName().toString();
    private static final String ACTION_SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    private Context mContext;
    private Intent mIntent;

    // Retrieve SMS
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        mIntent = intent;

        String action = intent.getAction();

        if(action.equals(ACTION_SMS_RECEIVED)){

            String address, str = "";
            int contactId = -1;

            SmsMessage[] msgs = getMessagesFromIntent(mIntent);
            if (msgs != null) {
                for (int i = 0; i < msgs.length; i++) {
                    address = msgs[i].getOriginatingAddress();
                    contactId = ContactsUtils.getContactId(mContext, address, "address");
                    str += msgs[i].getMessageBody().toString();
                    str += "\n";
                }
            }   

            if(contactId != -1){
                showNotification(contactId, str);
            }

            // ---send a broadcast intent to update the SMS received in the
            // activity---
            Intent broadcastIntent = new Intent();
            broadcastIntent.setAction("SMS_RECEIVED_ACTION");
            broadcastIntent.putExtra("sms", str);
            context.sendBroadcast(broadcastIntent);
        }

    }

    public static SmsMessage[] getMessagesFromIntent(Intent intent) {
        Object[] messages = (Object[]) intent.getSerializableExtra("pdus");
        byte[][] pduObjs = new byte[messages.length][];

        for (int i = 0; i < messages.length; i++) {
            pduObjs[i] = (byte[]) messages[i];
        }
        byte[][] pdus = new byte[pduObjs.length][];
        int pduCount = pdus.length;
        SmsMessage[] msgs = new SmsMessage[pduCount];
        for (int i = 0; i < pduCount; i++) {
            pdus[i] = pduObjs[i];
            msgs[i] = SmsMessage.createFromPdu(pdus[i]);
        }
        return msgs;
    }

    /**
    * The notification is the icon and associated expanded entry in the status
    * bar.
    */
    protected void showNotification(int contactId, String message) {
        //Display notification...
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myexample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.WRITE_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_MMS" />
    <uses-permission android:name="android.permission.WRITE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:debuggable="true"
        android:icon="@drawable/ic_launcher_icon"
        android:label="@string/app_name" >

        <activity
            //Main activity...
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            //Activity 2 ...
        </activity>
        //More acitivies ...

        // SMS Receiver
        <receiver android:name="com.myexample.receivers.SMSReceiver" >
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

    </application>
</manifest>

However, I was wondering if you could send and receive MMS messages in a similar fashion. After doing some research, many examples provided on blogs simply pass an Intent to the native Messaging application. I am trying to send an MMS without leaving my application. There doesn't seem to be a standard way of sending and receiving MMS. Has anyone gotten this to work?

Also, I am aware that the SMS/MMS ContentProvider is not a part of the official Android SDK, but I was thinking someone may have been able to implement this. Any help is greatly appreciated.

Update

I have added a BroadcastReceiver to the AndroidManifest.xml file to receive MMS messages

<receiver android:name="com.sendit.receivers.MMSReceiver" >
    <intent-filter>
        <action android:name="android.provider.Telephony.WAP_PUSH_RECEIVED" />

        <data android:mimeType="application/vnd.wap.mms-message" />
    </intent-filter>
</receiver>

In the MMSReceiver class, the onReceive() method is only able to grab the phoneNumber that the message was sent from. How do you grab other important things from an MMS such as the file path to the media attachment (image/audio/video), or the text in the MMS?

MMSReceiver.java

public class MMSReceiver extends BroadcastReceiver {
    private final String DEBUG_TAG = getClass().getSimpleName().toString();
    private static final String ACTION_MMS_RECEIVED = "android.provider.Telephony.WAP_PUSH_RECEIVED";
    private static final String MMS_DATA_TYPE = "application/vnd.wap.mms-message";

     // Retrieve MMS
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        String type = intent.getType();

        if(action.equals(ACTION_MMS_RECEIVED) && type.equals(MMS_DATA_TYPE)){

            Bundle bundle = intent.getExtras();

            Log.d(DEBUG_TAG, "bundle " + bundle);
            SmsMessage[] msgs = null;
            String str = "";
            int contactId = -1;
            String address;

            if (bundle != null) {

                byte[] buffer = bundle.getByteArray("data");
                Log.d(DEBUG_TAG, "buffer " + buffer);
                String incomingNumber = new String(buffer);
                int indx = incomingNumber.indexOf("/TYPE");
                if(indx>0 && (indx-15)>0){
                    int newIndx = indx - 15;
                    incomingNumber = incomingNumber.substring(newIndx, indx);
                    indx = incomingNumber.indexOf("+");
                    if(indx>0){
                        incomingNumber = incomingNumber.substring(indx);
                        Log.d(DEBUG_TAG, "Mobile Number: " + incomingNumber);
                    }
                }

                int transactionId = bundle.getInt("transactionId");
                Log.d(DEBUG_TAG, "transactionId " + transactionId);

                int pduType = bundle.getInt("pduType");
                Log.d(DEBUG_TAG, "pduType " + pduType);

                byte[] buffer2 = bundle.getByteArray("header");      
                String header = new String(buffer2);
                Log.d(DEBUG_TAG, "header " + header);

                if(contactId != -1){
                    showNotification(contactId, str);
                }

                // ---send a broadcast intent to update the MMS received in the
                // activity---
                Intent broadcastIntent = new Intent();
                broadcastIntent.setAction("MMS_RECEIVED_ACTION");
                broadcastIntent.putExtra("mms", str);
                context.sendBroadcast(broadcastIntent);

            }
        }

    }

    /**
    * The notification is the icon and associated expanded entry in the status
    * bar.
    */
    protected void showNotification(int contactId, String message) {
        //Display notification...
    }
}

According to the Documentation of android.provider.Telephony:

Broadcast Action: A new text based SMS message has been received by the device. The intent will have the following extra values:

pdus - An Object[] od byte[]s containing the PDUs that make up the message.

The extra values can be extracted using getMessagesFromIntent(android.content.Intent). If a BroadcastReceiver encounters an error while processing this intent it should set the result code appropriately.

@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";

Broadcast Action: A new data based SMS message has been received by the device. The intent will have the following extra values:

pdus - An Object[] of byte[]s containing the PDUs that make up the message.

The extra values can be extracted using getMessagesFromIntent(android.content.Intent). If a BroadcastReceiver encounters an error while processing this intent it should set the result code appropriately.

@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String DATA_SMS_RECEIVED_ACTION = "android.intent.action.DATA_SMS_RECEIVED";

Broadcast Action: A new WAP PUSH message has been received by the device. The intent will have the following extra values:

transactionId (Integer) - The WAP transaction ID

pduType (Integer) - The WAP PDU type

header (byte[]) - The header of the message

data (byte[]) - The data payload of the message

contentTypeParameters (HashMap<String,String>) - Any parameters associated with the content type (decoded from the WSP Content-Type header)

If a BroadcastReceiver encounters an error while processing this intent it should set the result code appropriately. The contentTypeParameters extra value is map of content parameters keyed by their names. If any unassigned well-known parameters are encountered, the key of the map will be 'unassigned/0x...', where '...' is the hex value of the unassigned parameter. If a parameter has No-Value the value in the map will be null.

@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String WAP_PUSH_RECEIVED_ACTION = "android.provider.Telephony.WAP_PUSH_RECEIVED";

Update #2

I have figured out how to pass extras in a PendingIntent to be received by a BroadcastReceiver: Android PendingIntent extras, not received by BroadcastReceiver

However, the extra gets passed to the SendBroadcastReceiver not the SMSReceiver. How can I pass an extra to the SMSReceiver?

Update #3

Receiving MMS

So after doing more research I saw some suggestions of registering a ContentObserver. That way you can detect when there are any changes to the content://mms-sms/conversations Content Provider, consequently allowing you to detect incoming MMS. Here is the closest example to get this to work that I have found: Receiving MMS

However, there is a variable mainActivity of type ServiceController. Where is the ServiceController class implemented? Are there any other implementations of a registered ContentObserver?

Sending MMS

As for sending MMS, I have come across this example: Send MMS

The problem is that I tried running this code on my Nexus 4, which is on Android v4.2.2, and I am receiving this error:

java.lang.SecurityException: No permission to write APN settings: Neither user 10099 nor current process has android.permission.WRITE_APN_SETTINGS.

The error gets thrown after querying the Carriers ContentProvider in the getMMSApns() method of the APNHelper class.

final Cursor apnCursor = this.context.getContentResolver().query(Uri.withAppendedPath(Carriers.CONTENT_URI, "current"), null, null, null, null);

Apparently you can't Read APNs in Android 4.2

What is the alternative for all those applications which use mobile data to perform operations (like sending MMS) and don't know the default APN setting present in the device?

Update #4

Sending MMS

I have tried following this example: Send MMS

As @Sam suggested in his answer:

You have to add jsoup to the build path, the jar to the build path and import com.droidprism.*; To do that in android, add the jars to the libs directory first, then configure the project build path to use the jars already in the libs directory, then on the build path config click order and export and check the boxes of the jars and move jsoup and droidprism jar to the top of the build order.

So now I no longer get the SecurityException errors. I am testing now on a Nexus 5 on Android KitKat. After running the sample code it gives me a 200 response code after the call to

MMResponse mmResponse = sender.send(out, isProxySet, MMSProxy, MMSPort);

However, I checked with the person I tried sending the MMS to. And they said they never received the MMS.

解决方案

I had the exact same problem you describe above (Galaxy Nexus on t-mobile USA) it is because mobile data is turned off.

In Jelly Bean it is: Settings > Data Usage > mobile data

Note that I have to have mobile data turned on PRIOR to sending an MMS OR receiving one. If I receive an MMS with mobile data turned off, I will get the notification of a new message and I will receive the message with a download button. But if I do not have mobile data on prior, the incoming MMS attachment will not be received. Even if I turn it on after the message was received.

For some reason when your phone provider enables you with the ability to send and receive MMS you must have the Mobile Data enabled, even if you are using Wifi, if the Mobile Data is enabled you will be able to receive and send MMS, even if Wifi is showing as your internet on your device.

It is a real pain, as if you do not have it on, the message can hang a lot, even when turning on Mobile Data, and might require a reboot of the device.

这篇关于发送和接收短信和彩信的Andr​​oid(pre奇巧的Andr​​oid 4.4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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