使用Android许可证服务器的奇怪错误

时间:2016-10-18 19:26:37

标签: android android-studio android-intent

因此,按照官方Android开发者网站的指示,我已经设置了所有内容以创建许可证检查,但我遇到了最奇怪的错误......

我已经为许可证服务器的3个可信响应创建了3个活动:

-EverythingOkActivity

-NotLicensedActivity

-ErrorActivity

每个应该根据服务器的响应启动,但是......

当我连接手机以调试我的应用时,如果满足启动 ErrorActivity 的条件,则会启动 NotLicensedActivity 意图,反之亦然,如果条件为启动 NotLicensedActivity 意图,然后启动 ErrorActivity 意图。

另外,我无法启动 EverythingOkActivity ,我已经在开发者控制台上设置了我的测试gmail帐户,并在许可测试响应中选择了 LICENSED 在我的帐户中,我还上传了APK。

调试控制台显示来自服务器的响应:

I/LicenseChecker: Received response.
I/LicenseChecker: Clearing timeout.

但至少没有错误,我没有互联网,在这种情况下显示:

I/LicenseChecker: Received response.
I/LicenseChecker: Clearing timeout.
W/LicenseValidator: Error contacting licensing server.

然后将意图发送到 NotLicensedActivity 而不是 ErrorActivity

我不知道出了什么问题,对不起,如果我是Android noob。

以下是我的主要活动的代码:

import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import com.google.android.vending.licensing.AESObfuscator;
import com.google.android.vending.licensing.LicenseChecker;
import com.google.android.vending.licensing.LicenseCheckerCallback;
import com.google.android.vending.licensing.ServerManagedPolicy;

public class MainActivity extends AppCompatActivity {
    private static final String BASE64_PUBLIC_KEY = "MIIBIjANBgkq[...]";
    private static final byte[] SALT = new byte[] {
            -44, 55, 30, -128, -103, -57, 74, -64, 51, 88, -95, -45, 77, -117, -36, -113, -11, 32, -64,
            89
    };
    private LicenseCheckerCallback mLicenseCheckerCallback;
    private LicenseChecker mChecker;
    private boolean keepGoing = true;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String deviceId = Settings.Secure.getString(getContentResolver(),
                Settings.Secure.ANDROID_ID);
        mLicenseCheckerCallback = new MyLicenseCheckerCallback();
        mChecker = new LicenseChecker(this, new ServerManagedPolicy(this,
                new AESObfuscator(SALT, getPackageName(), deviceId)),
                BASE64_PUBLIC_KEY);
        doCheck();
    }
    @Override
    public void onResume() {
        super.onResume();
        if (!keepGoing) {
            finish();
        }
    }
    private void doCheck() {
        mChecker.checkAccess(mLicenseCheckerCallback);
    }
    private class MyLicenseCheckerCallback implements LicenseCheckerCallback {
        public void allow(int policyReason) {
            if (isFinishing()) {
                // Don't update UI if Activity is finishing.
                return;
            }
            Intent intent = new Intent(MainActivity.this, EverythingOkActivity.class);
            startActivity(intent);
        }
        public void dontAllow(int policyReason) {
            if (isFinishing()) {
                return;
            }
            keepGoing = false;
            Intent intent = new Intent(MainActivity.this, NotLicensedActivity.class);
            startActivity(intent);
        }
        public void applicationError(int errorCode) {
            if (isFinishing()) {
                return;
            }
            keepGoing = false;
            Intent intent = new Intent(MainActivity.this, ErrorActivity.class);
            startActivity(intent);
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mChecker.onDestroy();  //Don't forget this line. Without it, your app might crash.
    }
}

1 个答案:

答案 0 :(得分:1)

我解决了!!!问题是应用程序需要至少作为Alpha在Play商店中发布,然后您需要向应用程序添加Alpha测试用户(在本例中为我自己)的列表。仅仅上传APK,您需要实际发布应用程序才能获得许可证。我将这里的代码作为一个例子,因为它就像一个魅力。干杯(^ _ ^)