我正在尝试实现LVL(Android的许可验证库),但在许可检查失败时遇到了问题。我弹出一个对话框,说明许可证检查失败,并向他们显示两个按钮。其中一个按钮是“重试”,我想立即进行另一次检查,或者是“购买应用程序”,它重定向他们在应用程序市场上购买应用程序。
当我点击重试按钮时,我可以从日志消息中看到我的许可检查被调用,我会收到另一个“不允许”回调,我会再次尝试显示上述失败对话框(我会看到onPreparedialog()再次被调用)。
问题是第二个对话框实际上没有显示。因此,即使许可证检查失败,用户也可以使用该应用程序。为什么对话不一次又一次地弹出?
我相信这是所有相关的准则:
private void checkLicense() {
Log.d(TAG, "Checking License...");
this.licenseChecker.checkAccess(this.licenseCheckerCallback);
}
private class MyLicenseCheckerCallback implements LicenseCheckerCallback {
public void allow() { }
public void dontAllow() {
Log.d(TAG, "License resposne: Don't Allow");
if (isFinishing())
return; // Don't update UI if Activity is finishing.
// Should not allow access. In most cases, the app should assume
// the user has access unless it encounters this. If it does,
// the app should inform the user of their unlicensed ways
// and then either shut down the app or limit the user to a
// restricted set of features.
// In this example, we show a dialog that takes the user to Market.
Log.d(TAG, "Showing No License Dialog");
showDialog(DIALOG_NO_LICENSE_ID);
}
public void applicationError(ApplicationErrorCode errorCode) {
if (isFinishing())
return; // Don't update UI if Activity is finishing.
// This is a polite way of saying the developer made a mistake
// while setting up or calling the license checker library.
showDialog(DIALOG_APPLICATION_ERROR_ID);
}
}
protected Dialog onCreateDialog(int id) {
Log.d(TAG, "Creating Dialog "+id);
switch(id) {
case DIALOG_NO_LICENSE_ID:
return this.makeRetryPurchaseDialog(getString(R.string.no_license));
case DIALOG_APPLICATION_ERROR_ID:
return this.makeRetryPurchaseDialog(this.applicationErrorMessageForDialog);
}
return null;
}
private Dialog makeRetryPurchaseDialog(String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(CalculatorTabActivity.this);
builder.setMessage(message).setCancelable(false)
.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
CalculatorTabActivity.this.checkLicense();
}
}).setNegativeButton("Purchase App", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
CalculatorTabActivity.this.openAppMarket();
}
});
return builder.create();
}