How to install Android Licensing into your App
To install android licensing into your application, you first need to set up the android vending licensing project in eclipse by folowing the instructions in this link:
http://developer.android.com/guide/publishing/licensing.html
Next you need to have the licensing permission in your manifest
<uses-permission android:name="com.android.vending.CHECK_LICENSE" />
Next you need to open your main activity class
import the licensing classes
import com.android.vending.licensing.LicenseChecker;
import com.android.vending.licensing.LicenseCheckerCallback;
then put at the top your public key and some random bytes that you made up. Your public key can be found in your profile configuration page: http://market.android.com/publish/editProfile
private static final String BASE64_PUBLIC_KEY = "YOUR PUBLIC KEY";// Generate your own 20 random bytes, and put them here.
private static final byte[] SALT = new byte[] {
-46, 65, 30, -128, -103, -57, 74, -64, 51, 88, -95, -45, 77, -117, -36, -113, -11, 32, -64, 89
};
Add your callback class implementation and access denied dialog to your activity file. The dialog allows users to redirect to purchase the application or exit.
private class MyLicenseCheckerCallback implements LicenseCheckerCallback {
public void allow() {
if (isFinishing()) {
// Don't update UI if Activity is finishing.
return;
}
// Should allow user access.
// so do nothing
}public void dontAllow() {
if (isFinishing()) {
// Don't update UI if Activity is finishing.
return;
}
displayResult(getString(R.string.dont_allow));
// Should not allow access. An app can handle as needed,
// typically by informing the user that the app is not licensed
// and then shutting down the app or limiting the user to a
// restricted set of features.
// In this example, we show a dialog that takes the user to Market.
showDialog(0);
}
}
protected Dialog onCreateDialog(int id) {
// We have only one dialog.
return new AlertDialog.Builder(this)
.setTitle(R.string.unlicensed_dialog_title)
.setMessage(R.string.unlicensed_dialog_body)
.setPositiveButton(R.string.buy_button, 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);
}
})
.setNegativeButton(R.string.quit_button, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.create();
}
Then add the following code to the topish of your onCreate() method (under the call to super) to run the check. Then test your licensing using the options given in your profile configuration page: http://market.android.com/publish/editProfile
// Try to use more data here. ANDROID_ID is a single point of attack.
String deviceId = Secure.getString(getContentResolver(), Secure.ANDROID_ID);// Library calls this when it's done.
mLicenseCheckerCallback = new MyLicenseCheckerCallback();
// Construct the LicenseChecker with a policy.
mChecker = new LicenseChecker(
this, new ServerManagedPolicy(this, new AESObfuscator(SALT, getPackageName(), deviceId)),
BASE64_PUBLIC_KEY);
doCheck();
Then every time your application's onCreate() method is called your app will check for the valid license. This will be seamless to someone who has purchased the app, otherwise a notification will pop up giving the user the option to buy the app or exit.
What if your user is offline (no data connection)?
Not to worry, this is actually dealt with by the licensing library which caches the response and has a timeout function built in.