Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android licensing application not works?

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.

like image 218
Khan Avatar asked Apr 23 '13 12:04

Khan


People also ask

How do I license Android apps?

Google Play offers a licensing service that lets you enforce licensing policies for applications that you publish on Google Play. With Google Play Licensing, your application can query Google Play at run time to obtain the licensing status for the current user, then allow or disallow further use as appropriate.

What happens when an application is licensed?

Software licenses typically provide end users with the right to one or more copies of the software without violating copyrights. The license also defines the responsibilities of the parties entering into the license agreement and may impose restrictions on how the software can be used.

How do I install Google Play licensing library?

Right click on your app that you are adding licensing to, and click properties, then hit Android . Go to the bottom and click library and add it to the build path. This should import the library to the Android Dependencies folder.


1 Answers

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

like image 162
Khan Avatar answered Oct 19 '22 22:10

Khan