Android SMS Message delivery report intent

后端 未结 2 749
抹茶落季
抹茶落季 2020-12-08 22:52

Android isn\'t firing the delivery intent when sending a text message. I\'m testing this on Android 2.2 on the HTC EVO 4G.

This is the current code. I\'m seeing \"SM

相关标签:
2条回答
  • 2020-12-08 23:03

    This is a very late answer. but, it may help someone.

    that code which is in question works fine, but, only change required is change the delivery request code. both are not to be the same request codes.

    Here it is... and run on a real device to see delivery report.

    EditText edNumber = findViewById(R.id.edNumber);
    EditText edMessage = findViewById(R.id.edMessage);
    
    String number = edNumber.getText().toString().trim();
    String message = edMessage.getText().toString().trim();
    
    // set pendingIntent for sent & delivered
    
            PendingIntent sentIntent = PendingIntent.getBroadcast(this, 100, new 
    Intent(SENT_ACTION), 0);
    
            PendingIntent deliveryIntent = PendingIntent.getBroadcast(this, 200, new 
    Intent(DELIVERY_ACTION), 0);
    
            registerReceiver(new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    Log.d("SMS ", "sent");
                }
            }, new IntentFilter(SENT_ACTION));
    
            registerReceiver(new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    Log.d("SMS ", "delivered");
                }
            }, new IntentFilter(DELIVERY_ACTION));
    
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(number, null, message, sentIntent, 
    deliveryIntent);
    
    0 讨论(0)
  • 2020-12-08 23:17

    Call this method where you want to send Sms

    private String SimState = "";
    private String address = ""; // Recipient Phone Number
    private String message = ""; // Message Body
    
    private void sendSms() {
        if (isSimExists()) {
            try {
                String SENT = "SMS_SENT";
    
                PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
    
                registerReceiver(new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context arg0, Intent arg1) {
                        int resultCode = getResultCode();
                        switch (resultCode) {
                            case Activity.RESULT_OK:
                                Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_LONG).show();
                                break;
                            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                                Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_LONG).show();
                                break;
                            case SmsManager.RESULT_ERROR_NO_SERVICE:
                                Toast.makeText(getBaseContext(), "No service", Toast.LENGTH_LONG).show();
                                break;
                            case SmsManager.RESULT_ERROR_NULL_PDU:
                                Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_LONG).show();
                                break;
                            case SmsManager.RESULT_ERROR_RADIO_OFF:
                                Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_LONG).show();
                                break;
                        }
                    }
                }, new IntentFilter(SENT));
    
                SmsManager smsMgr = SmsManager.getDefault();
                smsMgr.sendTextMessage(address, null, message, sentPI, null);
            } catch (Exception e) {
                Toast.makeText(this, e.getMessage() + "!\n" + "Failed to send SMS", Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
        } else {
            Toast.makeText(this, SimState + " " + "Cannot send SMS", Toast.LENGTH_LONG).show();
        }
    }
    
    
    // For receiving sms
    
    class SMSReceiver extends BroadcastReceiver {
        private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
    
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null && intent.getAction() != null && ACTION.compareToIgnoreCase(intent.getAction()) == 0) {
                // Sms Received Your code here
            }
        }
    }
    

    Note: You have to specify android.permission.SEND_SMS and android.permission.RECEIVE_SMS permissions in manifest file and also the receiver

        <receiver android:name=".SMSReceiver"  android:enabled="true">
                <intent-filter>
                    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                </intent-filter>
            </receiver>
    

    AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.tekeli.order"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk android:minSdkVersion="11" />
        <uses-permission android:name="android.permission.SEND_SMS" ></uses-permission>
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <activity
                android:name=".ActivityOrderActivity"
                android:label="@string/app_name">
                <intent-filter >
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".B" ></activity>
             <activity android:name=".C"></activity>
        </application>
    
    </manifest>
    
    0 讨论(0)
提交回复
热议问题