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.
See Question&Answers more detail:
os