Practical way to find out if SMS has been sent

后端 未结 3 1431
闹比i
闹比i 2020-11-29 04:16

I am interested in how I can figure out if SMS has been sent from the device.

In order to get notification when SMS is recieved, we use a broadcaster with:



        
3条回答
  •  甜味超标
    2020-11-29 04:53

    use the following method to send sms as well as check whether the sms get delivered or not.

    send.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    String phoneNo = "Phone number to sent";
                    String message = "Your message";
                    if (phoneNo.length() > 0 && message.length() > 0) {
                        TelephonyManager telMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                        int simState = telMgr.getSimState();
                        switch (simState) {
                        case TelephonyManager.SIM_STATE_ABSENT:
                            displayAlert();
                            break;
                        case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
                            // do something
                            break;
                        case TelephonyManager.SIM_STATE_PIN_REQUIRED:
                            // do something
                            break;
                        case TelephonyManager.SIM_STATE_PUK_REQUIRED:
                            // do something
                            break;
                        case TelephonyManager.SIM_STATE_READY:
                            // do something
                            sendSMS(phoneNo, message); // method to send message
                            break;
                        case TelephonyManager.SIM_STATE_UNKNOWN:
                            // do something
                            break;
                        }
    
                    } else {
                        Toast.makeText(getBaseContext(),
                                "Please enter both phone number and message.",
                                Toast.LENGTH_SHORT).show();
                    }
    
                }
    
                private void displayAlert() {
                    // TODO Auto-generated method stub
    
                    new AlertDialog.Builder(YourActivity.this)
                            .setMessage("Sim card not available")
                            .setCancelable(false)
                            // .setIcon(R.drawable.alert)
                            .setPositiveButton("ok",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog,
                                                int id) {
                                            Log.d("I am inside ok", "ok");
                                            dialog.cancel();
                                        }
                                    })
    
                            .show();
    
                }
    
            });
    
    
    
    
                private void sendSMS(String phoneNumber, String message) {
            String SENT = "SMS_SENT";
            String DELIVERED = "SMS_DELIVERED";
    
            PendingIntent sentPI = PendingIntent.getBroadcast(YourActivity.this, 0,
                    new Intent(SENT), 0);
    
            PendingIntent deliveredPI = PendingIntent.getBroadcast(YourActivity.this,
                    0, new Intent(DELIVERED), 0);
    
            // ---when the SMS has been sent---
            final String string = "deprecation";
            registerReceiver(new BroadcastReceiver() {
    
                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(YourActivity.this, "SMS sent",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(YourActivity.this, "Generic failure",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(YourActivity.this, "No service",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(YourActivity.this, "Null PDU",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off",
                                Toast.LENGTH_SHORT).show();
                        break;
    
                    }
                }
            }, new IntentFilter(SENT));
    
            // ---when the SMS has been delivered---
            registerReceiver(new BroadcastReceiver() {
                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(YourActivity.this, "SMS delivered",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(YourActivity.this, "SMS not delivered",
                                Toast.LENGTH_SHORT).show();
                        break;
                    }
                }
            }, new IntentFilter(DELIVERED));
    
            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
    
        }
    

提交回复
热议问题