Вопрос

I was reading sms messaging in android. There i got the code but i am confused or not really understanding it properly.

public class SMSActivity extends Activity 
{     
   String SENT = “SMS_SENT”;
   String DELIVERED = “SMS_DELIVERED”;  
   PendingIntent sentPI, deliveredPI; 
   BroadcastReceiver smsSentReceiver, smsDeliveredReceiver

    @Override     
   public void onCreate(Bundle savedInstanceState) 
  {         super.onCreate(savedInstanceState);  
     setContentView(R.layout.main);
    sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
    deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
  }

 @Override 
 public void onResume() 
 {         
   super.onResume();
   smsSentReceiver = new BroadcastReceiver()
             {             
                @Override             
              public void onReceive(Context arg0, Intent arg1) 
                  {                 
                   switch (getResultCode())
                     {             
                      case Activity.RESULT_OK: Toast.makeText(getBaseContext(), “SMS sent”, Toast.LENGTH_SHORT).show();   break;       
                      case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
                            Toast.makeText(getBaseContext(), “Genericfailure”,Toast.LENGTH_SHORT).show(); break;
                       }}
               };

     smsDeliveredReceiver = new BroadcastReceiver()
       {            
        @Override             
        public void onReceive(Context arg0, Intent arg1) 
            {                 
             switch (getResultCode())                 
              {                 
              case Activity.RESULT_OK:                     
                 Toast.makeText(getBaseContext(), “SMS delivered”, Toast.LENGTH_SHORT).show(); break;                 
              case Activity.RESULT_CANCELED: 
                   Toast.makeText(getBaseContext(), “SMS not delivered”, Toast.LENGTH_SHORT).show(); break;                
              }             
            }          

registerReceiver(smsDeliveredReceiver, new IntentFilter(DELIVERED));                 
registerReceiver(smsSentReceiver, new IntentFilter(SENT));     }

 //button click event method
 public void onClick(View v) 
  {  sendSMS(“5556”, “Hello my friends!”);     }

 private void sendSMS(String phoneNumber, String message)     
 {         SmsManager sms = SmsManager.getDefault();          
     sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);     
 } 
}

Here i have 2 pending intents: sentPI & deliveredPI, which i am using inside the sendTextMessage(); which means i am sending the pending intents to smsManager class.

And i have registered 2 broadcast recievers, which will recieve through the intent thata matches SMS_SENT & SMS_DELIVERED. In the book its said that the SMS_SENT & SMS_DELIVERED will be fired by the SmsManager, when the message is sent or delivered.DOes it mean ill get a intent msg of SMS_SENT & SMS_DELIVERED from SmsManger? or is it the intents that i m sending in sendTextMessage? Confused..... SomeOne plz help me? plz make me understand the cycle?

Это было полезно?

Решение

PendingIntents are given to other apps/processes as a way for them to send a message back to your own app as if it were your own.

SmsManager will handle the two PIs that you give it and do this: First, when SmsManager can confirm that the SMS was put into the network ("sent") it will broadcast your "SMS_SENT" pending intent so that you can update your SMS database that the message has gone out.

Second, once the network notifies SmsManager that the SMS was delivered (if the network supports this feature and there is a cost on some networks for this feature), then SmsManager will send your "SMS_DELIVERED" PI to your app so you can notify the user/update the SMS database.

You should have an option for the user to enable/disable DELIVERY notification because of the costs.

Also, it's these lines that create the PendingIntents:

sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);

But it doesn't matter what they are called. It's this line that identifies one as "sent" and the other "delivered":

 sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);     

another way of writing it:

 sms.sendTextMessage(phoneNumber, null, message, SMS_SENT_CONFIRMATION, SMS_DELIVER_CONFIRMATION);     

SmsManager stores whatever you put in SMS_SENT_CONFIRMATION until the message is sent. And the same with SMS_DELIVER_CONFIRMATION - create the intent, put whatever you want in it and then it will send it back to your app. You might put a message ID, for example, to track your confirmations.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top