我有一个AppWidget这个应用程序,当用户点击它时,它会发送一个短信。通过单击小部件发送SMS工作正常。现在,我想知道SMS的发送是成功还是失败。我该怎么做?
以下是发送短信的代码
String sms2 = id + " " + name + " " + cn.getName() + " " + message
+ " " + lati + " " + longi
+ " https://maps.google.com/?q=" + lati + "," + longi + " -alertoapp";
String cp = cn.getPhoneNumber();
PendingIntent piSent=PendingIntent.getBroadcast(context, 0, new Intent("SMS_SENT"), 0);
PendingIntent piDelivered=PendingIntent.getBroadcast(context, 0, new Intent("SMS_DELIVERED"), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(cp, null, sms2, piSent, piDelivered);
清单
<receiver android:name=".Widget" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<!-- Broadcast Receiver that will also process our self created action -->
<action android:name="de.thesmile.android.widget.buttons.ButtonWidget.ACTION_WIDGET_RECEIVER"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_provider" />
</receiver>
我想使用下面的方法来解决是否发送SMS的问题,但问题是在BroadCastReceiver类中没有registerReceiver方法。
switch (getResultCode()) {
case Activity.RESULT_OK:
clear1();
clear2();
clear3();
clear4();
Toast.makeText(getBaseContext(), "SMS has been sent", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic Failure", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No Service", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio Off", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getApplicationContext(), "Coordinates is null, Try again", Toast.LENGTH_LONG).show();
break;
}
}
};
smsDeliveredReceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
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(smsSentReceiver, new IntentFilter("SMS_SENT"));
registerReceiver(smsDeliveredReceiver, new IntentFilter("SMS_DELIVERED"));
有什么建议吗?
答案 0 :(得分:1)
对于那些遇到同样问题的人,我已经在this
的帮助下解决了这个问题。所以这是
smsSentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context ctx, Intent arg1) {
String msg = "";
switch (getResultCode()) {
case Activity.RESULT_OK:
msg = "SMS has been sent";
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
msg = "Generic Failure";
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
msg = "No Service";
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
msg = "Null PDU";
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
msg = "Radio Off";
break;
default:
msg = "Coordinates is null, Try again";
break;
}
Toast.makeText(ctx.getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
};
smsDeliveredReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context ctx, Intent arg1) {
String msg = "";
switch (getResultCode()) {
case Activity.RESULT_OK:
msg = "SMS Delivered";
break;
case Activity.RESULT_CANCELED:
msg = "SMS not delivered";
break;
}
Toast.makeText(ctx.getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
};
ctx.getApplicationContext()
.registerReceiver(smsSentReceiver, new IntentFilter("SMS_SENT"));
ctx.getApplicationContext()
.registerReceiver(smsDeliveredReceiver, new IntentFilter("SMS_DELIVERED"));
结论:因此,要在BroadcastReceiver类中注册接收器。
而不是registerReceiver(smsSentReceiver, new IntentFilter("SMS_SENT"));
使用context.getApplicationContext().registerReceiver(smsSentReceiver, new IntentFilter("SMS_SENT"));