Reputation: 123
So following the instruction from the official android developers website I've set up everything to create a license check, but I am having the weirdest of the errors...
I've created 3 activities for the 3 posible responses from the license server:
-EverythingOkActivity
-NotLicensedActivity
-ErrorActivity
Each one should be launched depending on the server's response, But...
When I connect my phone to debug my app, if the condition to launch the ErrorActivity is meet, then it launches the NotLicensedActivity intent and viceversa, if the condition to launch the NotLicensedActivity intent is meet, then it launches the ErrorActivity intent.
Plus I am never able to launch the EverythingOkActivity, I've already setup my test gmail account on the developer console and selected LICENSED at the license test response on my account, I've also uploaded the APK.
The debug console shows this response from the server:
I/LicenseChecker: Received response.
I/LicenseChecker: Clearing timeout.
But no errors at least I have no internet, in that case is shows:
I/LicenseChecker: Received response.
I/LicenseChecker: Clearing timeout.
W/LicenseValidator: Error contacting licensing server.
And then launches the intent to NotLicensedActivity instead of ErrorActivity
I have no idea what could be wrong, Excuse me if I am Android noob.
Here is the code of my Main Activity:
import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
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 MainActivity extends AppCompatActivity {
private static final String BASE64_PUBLIC_KEY = "MIIBIjANBgkq[...]";
private static final byte[] SALT = new byte[] {
-44, 55, 30, -128, -103, -57, 74, -64, 51, 88, -95, -45, 77, -117, -36, -113, -11, 32, -64,
89
};
private LicenseCheckerCallback mLicenseCheckerCallback;
private LicenseChecker mChecker;
private boolean keepGoing = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String deviceId = Settings.Secure.getString(getContentResolver(),
Settings.Secure.ANDROID_ID);
mLicenseCheckerCallback = new MyLicenseCheckerCallback();
mChecker = new LicenseChecker(this, new ServerManagedPolicy(this,
new AESObfuscator(SALT, getPackageName(), deviceId)),
BASE64_PUBLIC_KEY);
doCheck();
}
@Override
public void onResume() {
super.onResume();
if (!keepGoing) {
finish();
}
}
private void doCheck() {
mChecker.checkAccess(mLicenseCheckerCallback);
}
private class MyLicenseCheckerCallback implements LicenseCheckerCallback {
public void allow(int policyReason) {
if (isFinishing()) {
// Don't update UI if Activity is finishing.
return;
}
Intent intent = new Intent(MainActivity.this, EverythingOkActivity.class);
startActivity(intent);
}
public void dontAllow(int policyReason) {
if (isFinishing()) {
return;
}
keepGoing = false;
Intent intent = new Intent(MainActivity.this, NotLicensedActivity.class);
startActivity(intent);
}
public void applicationError(int errorCode) {
if (isFinishing()) {
return;
}
keepGoing = false;
Intent intent = new Intent(MainActivity.this, ErrorActivity.class);
startActivity(intent);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mChecker.onDestroy(); //Don't forget this line. Without it, your app might crash.
}
}
Upvotes: 1
Views: 163
Reputation: 123
I solved!!! The problem is the app needs to be published in the Play Store at least as an Alpha and then you need to add a list a Alpha-tester users (in this case myself) to the app. Is not enough to to upload the APK, you need to actually publish the app to get the license to work. I will let that code here as a example, since it works like a charm. Cheers (^_^)
Upvotes: 1