在我的项目中,我使用SmsManager
向我的客户发送消息。如果sms成功发送,我想更改客户端表中的客户端状态。这个名为PassengerInformation.java的类是activity
,用于发送短信并更改db中客户端的状态。我发现有一个问题是PassengerInformation活动运行代码更新客户端状态而不知道sms发送成功,它必须等待运行更新代码,直到SmsManager
成功发送短信。
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);
}
}
答案 0 :(得分:1)
我发现有一个问题是PassengerInformation活动运行代码更新客户端的状态而不知道sms成功发送,它必须等待运行更新代码,直到Sms成功发送sms .--&gt;为此,您必须将更新数据库代码保存在SendSMS类的switch / case中。
case Activity.RESULT_OK:
Toast.makeText(mContext, "SMS sent", Toast.LENGTH_SHORT).show();
PassengerInformation.messageSending=true;
if(messageSending){
//update database
}
break;