Question

I implemented Google License checker by reading the official instructions.

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();
    }
}

and here is the manifest permission which I given

<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"/>

after implementing this I run app in and sign it with with release key and uploaded apk as draft in google play developer console and I have also added 3 test account in setting of developer console.

As per I read and understand the must be run in the device which having the test account as main account in device and rest of the device it would have to show the dialog.

But in my case in all device it will works fine and this thing happen some strange situation if internet is disconnected than it will show dialog about piracy and if it is connected than it worsk in all device inspite of the test account devices.

Was it helpful?

Solution

Finally I found the proper way of testing of google license checker.

As I uploaded apk in google play developer console as draft and If I install that apk in any device than it will run with response LICENSED if in the device is not having login with test account.

And finally I do testing this way I added test account and i select License Test Response in developer console as LICENSED than app will run and after i also change setting in developer console with NOT_LICENSED than it shows dialog about piracy in the device which having test account as main account.

IMPORTANT NOTE :And other than test account devices it run with the response LICENSED beacuse is I uploaded APK as draft in play store.So for the testing google licensing we must have to consider only test account added in Google Play Developer Console

OTHER TIPS

You already done all these steps but clearly check it once again:

  1. check once your's public key compare with the draft store public key.
  2. Give own salt 20 numbers.
  3. Give properly test accounts in the google play and same account use in the device also.
  4. In google play you have to put licensed mode for application draft.
  5. check after some time because it will take some time to give valid results.
  6. finally verify the alert dialog conditions in application.
  7. clear the google play cache in your mobile may be it will store the results.

The LVL is working on all devices with the accounts You added, not only the main account. It should not work on phones with an account You don´t added to the developer console. Even, the LVL tries to verify Your app with the google LVL -Server, so You need an internet connection, this is ok. If You just want to check the license once, You have to save some data for example in the shared preferences, that the check has already done. So, the next time the app starts, you could check with these saved shared preferences, if the app is just verified. If yes, do no check again, if no, try it again.

You can set the behavior of checking license in the developer console, You can simulate a normal response, or for example a "not verified" response and so on....

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top