Practical way to find out if SMS has been sent

后端 未结 3 1409
闹比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:44

    One thing that comes to mind is using the logcat log. Every time a SMS is sent/received there is a specific entry. Just hook up your phone in debug and test and you will observe the specific entry.

    I should mention this method is done from your app, so you can work it into your code.

    Take a pull of this. There may be other methods but that seems fairly easy and do-able.

    0 讨论(0)
  • 2020-11-29 04:45

    Yes, It is possible to listen SMS ContentProvider by using ContentObserver

    Here is my example for Outgoing SMS:

    First register a ContetObserver with content://sms/

       public class Smssendservice extends Service{
    
           @Override  
           public void onCreate() {
                SmsContent content = new SmsContent(new Handler());  
                // REGISTER ContetObserver 
                this.getContentResolver().
                    registerContentObserver(Uri.parse("content://sms/"), true, SMSObserver);  
           } 
    
           @Override
           public IBinder onBind(Intent arg0) {
                // TODO Auto-generated method stub
    
                return null;
           }
    

    SMSObserver.class

           public class SMSObserver extends ContentObserver {
                private Handler m_handler = null;
    
                public SMSObserver(SMSLogger handler){
                     super(handler);
                     m_handler = handler;
                }
    
                @Override
                public void onChange(boolean selfChange) {
                super.onChange(bSelfChange);
                Uri uriSMSURI = Uri.parse("content://sms");
    
                Cursor cur = this.getContentResolver().query(uriSMSURI, null, null,
                     null, null);
                cur.moveToNext();
    
                String protocol = cur.getString(cur.getColumnIndex("protocol"));
    
                if(protocol == null) {
             //the message is sent out just now     
                }               
                else {
                     //the message is received just now   
                }
          }
      }
    
    }
    
    0 讨论(0)
  • 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);
    
        }
    
    0 讨论(0)
提交回复
热议问题