发送短信时查看确认充值计费对话框

本教程将介绍发送短信时查看确认充值计费对话框的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。

发送短信时查看确认充值计费对话框 教程 第1张

问题描述

在以下代码中使用SMS管理器发送SMS:

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null,message, null, null);

在下面的对话框中,当设备Android版本4.3或4.4时,我会收到它。
如果单击"发送"或"取消",怎么检测状态?

推荐答案

只要您阅读相应的源代码,就会发现如果单击"发送"或"取消",您就检测不到状态。

"向高级短信短代码(或可疑的高级短信号码)发送消息的默认行为是要求用户确认。允许用户设置默认策略("记住此选择"复选框),以始终/永远不允许应用程序将来向高级短信代码发送短信。该策略可由应用程序信息屏幕中的设置应用程序更改。"
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);
}

当您单击对话框上的Send或Cancel按钮时,它将向其线程中的处理程序发送一条消息。但是您似乎没有办法捕获单击事件。

好了关于发送短信时查看确认充值计费对话框的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。