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);
}
}
如果移動代碼的情況下更新表Activity.RESULT_OK倒不如: 或其他如使用情況,並在短信如果布爾返回true,則更新表發送代碼的最後返回布爾值,希望這會有所幫助 – mushahid 2014-10-28 05:40:41