在以下代码中使用SMS Manager发送短信:
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null,message, null, null);
在下面的对话框中,当设备安卓版4.3或4.4时我收到它。 如果单击发送或取消,如何检测状态?
答案 0 :(得分:1)
如果您阅读相应的源代码,只要单击“发送”或“取消”,您就会发现无法检测到状态。
“将消息发送到高级短信短代码(或可疑的高级短信号码)的默认行为是要求用户进行确认。允许用户设置默认策略(”记住此选项“复选框) /永远不允许应用程序在将来向优质短代码发送短信。可以通过应用信息屏幕中的设置应用更改政策。“ See here for details
对话框定义于 src / java / com / android / internal / telephony / SMSDispatcher.java
/**
* Post an alert when SMS needs confirmation due to excessive usage.
* @param tracker an SmsTracker for the current message.
*/
protected void handleReachSentLimit(SmsTracker tracker) {
if (denyIfQueueLimitReached(tracker)) {
return; // queue limit reached; error was returned to caller
}
CharSequence appLabel = getAppLabel(tracker.mAppInfo.packageName);
Resources r = Resources.getSystem();
Spanned messageText = Html.fromHtml(r.getString(R.string.sms_control_message, appLabel));
ConfirmDialogListener listener = new ConfirmDialogListener(tracker);
AlertDialog d = new AlertDialog.Builder(mContext)
.setTitle(R.string.sms_control_title)
.setIcon(R.drawable.stat_sys_warning)
.setMessage(messageText)
.setPositiveButton(r.getString(R.string.sms_control_yes), listener)
.setNegativeButton(r.getString(R.string.sms_control_no), listener)
.setOnCancelListener(listener)
.create();
d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
d.show();
}
负面和正面按钮的回调:
/**
* Dialog listener for SMS confirmation dialog.
*/
private final class ConfirmDialogListener
implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener,
CompoundButton.OnCheckedChangeListener {
private final SmsTracker mTracker;
private Button mPositiveButton;
private Button mNegativeButton;
private boolean mRememberChoice; // default is unchecked
ConfirmDialogListener(SmsTracker tracker) {
mTracker = tracker;
}
void setPositiveButton(Button button) {
mPositiveButton = button;
}
void setNegativeButton(Button button) {
mNegativeButton = button;
}
@Override
public void onClick(DialogInterface dialog, int which) {
// Always set the SMS permission so that Settings will show a permission setting
// for the app (it won't be shown until after the app tries to send to a short code).
int newSmsPermission = SmsUsageMonitor.PREMIUM_SMS_PERMISSION_ASK_USER;
if (which == DialogInterface.BUTTON_POSITIVE) {
Log.d(TAG, "CONFIRM sending SMS");
// XXX this is lossy- apps can have more than one signature
EventLog.writeEvent(EventLogTags.SMS_SENT_BY_USER,
mTracker.mAppInfo.signatures[0].toCharsString());
sendMessage(obtainMessage(EVENT_SEND_CONFIRMED_SMS, mTracker));
if (mRememberChoice) {
newSmsPermission = SmsUsageMonitor.PREMIUM_SMS_PERMISSION_ALWAYS_ALLOW;
}
} else if (which == DialogInterface.BUTTON_NEGATIVE) {
Log.d(TAG, "DENY sending SMS");
// XXX this is lossy- apps can have more than one signature
EventLog.writeEvent(EventLogTags.SMS_DENIED_BY_USER,
mTracker.mAppInfo.signatures[0].toCharsString());
sendMessage(obtainMessage(EVENT_STOP_SENDING, mTracker));
if (mRememberChoice) {
newSmsPermission = SmsUsageMonitor.PREMIUM_SMS_PERMISSION_NEVER_ALLOW;
}
}
setPremiumSmsPermission(mTracker.mAppInfo.packageName, newSmsPermission);
}
当您单击对话框上的“发送”或“取消”按钮时,它将在其线程中向其处理程序发送消息。但似乎没有办法捕获点击事件。