Sending and Receiving SMS and MMS in Android (pre Kit Kat Android 4.4)

前端 未结 6 573
悲&欢浪女
悲&欢浪女 2020-11-21 22:51

I have figured out how to send and receive SMS messages. To send SMS messages I had to call the sendTextMessage() and sendMultipartTextMessage() m

相关标签:
6条回答
  • 2020-11-21 22:55

    I dont understand the frustrations. Why not just make a broadcastreceiver that filters for this intent:

    android.provider.Telephony.MMS_RECEIVED
    

    I checked a little further and you might need system level access to get this (rooted phone).

    0 讨论(0)
  • 2020-11-21 22:56

    To send an mms for Android 4.0 api 14 or higher without permission to write apn settings, you can use this library: Retrieve mnc and mcc codes from android, then call

    Carrier c = Carrier.getCarrier(mcc, mnc);
    if (c != null) {
        APN a = c.getAPN();
        if (a != null) {
            String mmsc = a.mmsc;
            String mmsproxy = a.proxy; //"" if none
            int mmsport = a.port; //0 if none
        }
    }
    

    To use this, add Jsoup and droid prism jar to the build path, and import com.droidprism.*;

    0 讨论(0)
  • 2020-11-21 23:05

    SmsListenerClass

    public class SmsListener extends BroadcastReceiver {
    
    static final String ACTION =
            "android.provider.Telephony.SMS_RECEIVED";
    
    @Override
    public void onReceive(Context context, Intent intent) {
    
        Log.e("RECEIVED", ":-:-" + "SMS_ARRIVED");
    
        // TODO Auto-generated method stub
        if (intent.getAction().equals(ACTION)) {
    
            Log.e("RECEIVED", ":-" + "SMS_ARRIVED");
    
            StringBuilder buf = new StringBuilder();
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
    
                Object[] pdus = (Object[]) bundle.get("pdus");
    
                SmsMessage[] messages = new SmsMessage[pdus.length];
                SmsMessage message = null;
    
                for (int i = 0; i < messages.length; i++) {
    
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        String format = bundle.getString("format");
                        messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i], format);
                    } else {
                        messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                    }
    
                    message = messages[i];
                    buf.append("Received SMS from  ");
                    buf.append(message.getDisplayOriginatingAddress());
                    buf.append(" - ");
                    buf.append(message.getDisplayMessageBody());
                }
    
                MainActivity inst = MainActivity.instance();
                inst.updateList(message.getDisplayOriginatingAddress(),message.getDisplayMessageBody());
    
            }
    
            Log.e("RECEIVED:", ":" + buf.toString());
    
            Toast.makeText(context, "RECEIVED SMS FROM :" + buf.toString(), Toast.LENGTH_LONG).show();
    
        }
    }
    

    Activity

    @Override
    public void onStart() {
        super.onStart();
        inst = this;
    }
    
    public static MainActivity instance() {
        return inst;
    }
    
    public void updateList(final String msg_from, String msg_body) {
    
        tvMessage.setText(msg_from + " :- " + msg_body);
    
        sendSMSMessage(msg_from, msg_body);
    
    }
    
    protected void sendSMSMessage(String phoneNo, String message) {
    
        try {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(phoneNo, null, message, null, null);
            Toast.makeText(getApplicationContext(), "SMS sent.", Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "SMS faild, please try again.", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }
    

    Manifest

    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS"/>
    
    <receiver android:name=".SmsListener">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
    
    0 讨论(0)
  • 2020-11-21 23:12

    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.

    0 讨论(0)
  • 2020-11-21 23:14

    I dont think there is any sdk support for sending mms in android. Look here Atleast I havent found yet. But a guy claimed to have it. Have a look at this post.

    Send MMS from My application in android

    0 讨论(0)
  • 2020-11-21 23:16

    There is not official api support which means that it is not documented for the public and the libraries may change at any time. I realize you don't want to leave the application but here's how you do it with an intent for anyone else wondering.

    public void sendData(int num){
        String fileString = "..."; //put the location of the file here
        Intent mmsIntent = new Intent(Intent.ACTION_SEND);
        mmsIntent.putExtra("sms_body", "text");
        mmsIntent.putExtra("address", num);
        mmsIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(fileString)));
        mmsIntent.setType("image/jpeg");
        startActivity(Intent.createChooser(mmsIntent, "Send"));
    
    }
    

    I haven't completely figured out how to do things like track the delivery of the message but this should get it sent.

    You can be alerted to the receipt of mms the same way as sms. The intent filter on the receiver should look like this.

    <intent-filter>
        <action android:name="android.provider.Telephony.WAP_PUSH_RECEIVED" />
        <data android:mimeType="application/vnd.wap.mms-message" />
    </intent-filter>
    
    0 讨论(0)
提交回复
热议问题