8

我通过阅读官方说明实现了 Google 许可证检查器。

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings.Secure;
import android.widget.Toast;

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 Splash extends Activity {
    MyLicenseCheckerCallback mLicenseCheckerCallback;
    LicenseChecker mChecker;
    byte[] SALT = new byte[] { -73, 95, 70, -126, -103, -57, 14, -46, 51, 88, -5,
        -60, 77, -88, -63, -13, -1, 82, -4, 9 };
    //Handler mHandler;
    String BASE64_PUBLIC_KEY="My base key";
    Context mContext;
    IBinder serviceBinder;
    String deviceId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        mLicenseCheckerCallback = new MyLicenseCheckerCallback();
        deviceId = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
        // Construct the LicenseChecker with a policy.
        mChecker = new LicenseChecker(this,
            new ServerManagedPolicy(Splash.this, new AESObfuscator(SALT,
                getPackageName(), deviceId)), BASE64_PUBLIC_KEY);
        doCheck();
    }

    private class MyLicenseCheckerCallback implements LicenseCheckerCallback {
        @Override
        public void allow(int reason) {
            // TODO Auto-generated method stub
            if (isFinishing())
                return; // Don't update UI if Activity is finishing.
            // Toast.makeText(Splash.this, "Success", Toast.LENGTH_LONG).show();
            Intent intent=new Intent(Splash.this,Main.class);
            startActivity(intent);
            finish();

            // Should allow user access.
            // so do nothing
        }

        @Override
        public void dontAllow(int reason) {
            // TODO Auto-generated method stub
            if (isFinishing())
                return; // Don't update UI if Activity is finishing.
            // Toast.makeText(Splash.this, "Fail", Toast.LENGTH_LONG).show();
            createDialog();
        }

        @Override
        public void applicationError(int errorCode) {
            // TODO Auto-generated method stub
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mChecker.onDestroy();
    }

    private void doCheck() {
        // mCheckLicenseButton.setEnabled(false);
        setProgressBarIndeterminateVisibility(true);
        ///  mStatusText.setText(R.string.checking_license);
        mChecker.checkAccess(mLicenseCheckerCallback);
    }

    public void createDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("PIRACY WARNING");
        builder.setMessage("A valid purchase for My App has not been detected.  Your IP"
            + " has been logged and all offenders will be reported to the authorities."
            + " If you received this message in error, please contact Support.");
        builder.setPositiveButton("Buy Now", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(
                    "http://market.android.com/details?id=" + getPackageName()));
                startActivity(marketIntent);
            }
        });
        builder.setNegativeButton("Quit", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }
}

这是我给予的明确许可

<supports-screens android:normalScreens="true" android:largeScreens="true" android:smallScreens="true" android:anyDensity="true" android:xlargeScreens="true" android:resizeable="true"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="com.android.vending.CHECK_LICENSE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

实施此操作后,我运行应用程序并使用发布密钥对其进行签名,并将 apk 作为草稿上传到Google Play 开发者控制台中 ,并且我还在开发者控制台的设置中添加了 3 个测试帐户。

根据我的阅读和理解,必须在设备中运行测试帐户作为设备中的主帐户,而设备的其余部分则必须显示对话框。

但是在我的情况下,在所有设备上它都可以正常工作,并且如果互联网断开连接,它会显示有关盗版的对话框,并且如果它已连接,那么它会在所有设备中工作,尽管有测试帐户设备。

4

3 回答 3

2

最后我找到了测试谷歌许可证检查器的正确方法。

当我将 apk 作为草稿上传到 google play 开发者控制台时,如果我在任何设备中安装该 apk,如果设备中没有使用测试帐户登录,它将以响应LICENSED运行。

最后,我以这种方式进行测试,我添加了测试帐户,并在开发人员控制台中选择许可证测试响应作为LICENSED,然后应用程序将运行,并且在我还使用NOT_LICENSED更改开发人员控制台中的设置之后,它显示有关具有测试帐户的设备中的盗版的对话框作为主账户。

重要说明:除了测试帐户设备之外,它运行响应LICENSED因为我将 APK 作为草稿上传到 Play 商店。因此,对于测试 google 许可,我们必须考虑仅在 Google Play Developer Console 中添加测试帐户

于 2013-04-24T07:47:50.490 回答
1

您已经完成了所有这些步骤,但再次清楚地检查它:

  1. 将您的公钥与草稿存储公钥进行比较后检查。
  2. 给自己的盐 20 个数字。
  3. 在 google play 中提供正确的测试帐户,并在设备中使用相同的帐户。
  4. 在 google play 中,您必须为应用程序草稿设置许可模式。
  5. 一段时间后检查,因为需要一些时间才能给出有效的结果。
  6. 最后验证应用程序中的警报对话框条件。
  7. 清除手机中的谷歌播放缓存可能会存储结果。
于 2013-04-23T13:41:28.420 回答
0

LVL 正在使用您添加的帐户的所有设备上工作,而不仅仅是主帐户。它不适用于具有您未添加到开发者控制台的帐户的手机。甚至,LVL 尝试使用 google LVL -Server 验证您的应用程序,因此您需要互联网连接,这没关系。如果您只想检查一次许可证,您必须保存一些数据,例如在共享首选项中,检查已经完成。因此,下次启动应用程序时,您可以检查这些保存的共享首选项,如果应用程序刚刚通过验证。如果是,不要再检查,如果不是,再试一次。

您可以在开发者控制台中设置检查许可证的行为,您可以模拟正常响应,或者例如“未验证”响应等等......

于 2013-04-23T12:50:39.620 回答