2014-10-28 63 views
0

在我的项目中,我用SmsManager发送消息给我的客户。如果短信发送成功,我想更改客户端表中的客户端状态。这个名为PassengerInformation.java的类是activity,用于发送短信并更改db中客户端的状态。有一个问题,我发现PassengerInformation活动的客户端更新状态运行代码不知道sms是否成功发送,它必须等待运行代码的更新,直到短信成功发送SmsManager。任何建议表示赞赏。udpate数据库后发送短信

感谢,

PassengerInformation.java

public class PassengerInformation extends BaseFragment{ 
public static Boolean messageSending= false; 
@Override 
public void onViewCreated(View view, Bundle savedInstanceState) 
{ 
    SendSMS sms=new SendSMS(v.getContext()); 
    sms.sendSMS("83939420",builder.toString()); 
    if(messageSending){ 
     //update database 
    } 
} 

SendSMS.java

public class SendSMS { 
    Context mContext; 
    String SENT = "SMS_SENT"; 
    String DELIVERED = "SMS_DELIVERED"; 

    ArrayList<PendingIntent> sentIntents; 
    ArrayList<PendingIntent> deliveryIntents; 

public SendSMS(Context c){ 
    mContext=c; 
} 
public void sendSMS(String phoneNumber, String message) 
    {    

     SmsManager sm = SmsManager.getDefault(); 
     ArrayList<String> parts =sm.divideMessage(message); 
     int numParts = parts.size(); 

     sentIntents = new ArrayList<PendingIntent>(); 
     deliveryIntents = new ArrayList<PendingIntent>(); 

     for (int i = 0; i < numParts; i++) { 
      sentIntents.add(PendingIntent.getBroadcast(mContext, 0, new Intent(SENT), 0)); 
      deliveryIntents.add(PendingIntent.getBroadcast(mContext, 0, new Intent(DELIVERED), 0)); 
      } 
     //---when the SMS has been sent--- 
     mContext.registerReceiver(new BroadcastReceiver(){ 
      @Override 
      public void onReceive(Context arg0, Intent arg1) { 
       switch (getResultCode()) 
       { 
        case Activity.RESULT_OK: 
         Toast.makeText(mContext, "SMS sent", Toast.LENGTH_SHORT).show(); 
         PassengerInformation.messageSending=true; 
         break; 
        case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
         Toast.makeText(mContext, "Generic failure",Toast.LENGTH_SHORT).show(); 
         PassengerInformation.messageSending=false; 
         break; 
        case SmsManager.RESULT_ERROR_NO_SERVICE: 
         Toast.makeText(mContext, "No service",Toast.LENGTH_SHORT).show(); 
         PassengerInformation.messageSending=false; 
         break; 
        case SmsManager.RESULT_ERROR_NULL_PDU: 
         Toast.makeText(mContext, "Null PDU",Toast.LENGTH_SHORT).show(); 
         PassengerInformation.messageSending=false; 
         break; 
        case SmsManager.RESULT_ERROR_RADIO_OFF: 
         Toast.makeText(mContext, "Radio off",Toast.LENGTH_SHORT).show(); 
         PassengerInformation.messageSending=false; 
         break; 
       } 
      } 
     }, new IntentFilter(SENT)); 

     //---when the SMS has been delivered--- 
     mContext.registerReceiver(new BroadcastReceiver(){ 
      @Override 
      public void onReceive(Context arg0, Intent arg1) { 
       switch (getResultCode()) 
       { 
        case Activity.RESULT_OK: 
         Toast.makeText(mContext, "SMS delivered", 
           Toast.LENGTH_SHORT).show(); 
         PassengerInformation.messageSending=true; 
         break; 
        case Activity.RESULT_CANCELED: 
         Toast.makeText(mContext, "SMS not delivered", 
           Toast.LENGTH_SHORT).show(); 
         PassengerInformation.messageSending=false; 
         break;       
       } 
      } 
     }, new IntentFilter(DELIVERED));   

     SmsManager sms = SmsManager.getDefault(); 
     sms.sendMultipartTextMessage(phoneNumber, null, parts, sentIntents, deliveryIntents); 
    } 
    } 

回答

1

还有就是我发现了一个问题,那就是更新的状态PassengerInformation活动运行代码不知道短信的客户端发送成功,它必须等待runni ng代码的更新,直到SmsManager成功发送短信。 - >因为你必须保持你的更新数据库代码在开关/ SendSMS类的情况下。

case Activity.RESULT_OK: 
    Toast.makeText(mContext, "SMS sent", Toast.LENGTH_SHORT).show(); 
    PassengerInformation.messageSending=true; 

    if(messageSending){ 
     //update database 
     } 
break; 
+0

如果移动代码的情况下更新表Activity.RESULT_OK倒不如: 或其他如使用情况,并在短信如果布尔返回true,则更新表发送代码的最后返回布尔值,希望这会有所帮助 – mushahid 2014-10-28 05:40:41