So I am using flutter for app development (obviously, the title) and this is a paid app and so the license needs to be verified. I am using MedthodChannel for this purpose as I couldn't find a way in flutter itself.
Now the problem: So in Java side, I am checking for license verification and then the result is sent to flutter. That is how Method channel works. But the problem is that the result is sent to flutter even before the verification completes and because of which flutter part thinks the license is not verified even if it is.
I am attaching java side code below
package in.navkargems.diamondtools;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;
import com.google.android.vending.licensing.LicenseChecker;
import com.google.android.vending.licensing.LicenseCheckerCallback;
import com.google.android.vending.licensing.StrictPolicy;
import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "in.navkargems.diamondtools/verify";
private static final String _key_to_verify_base64 = "MIIBIjAN...";
private Handler mHandler;
private LicenseChecker mChecker;
private LicenseCheckerCallback mLicenseCheckerCallback;
boolean licensed;
boolean checkingLicense;
boolean didCheck;
int message = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
new MethodChannel.MethodCallHandler() {
@Override
public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
if (methodCall.method.equals("verifyLicense")) {
String deviceId = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
Log.i("Device Id", deviceId);
mHandler = new Handler();
mLicenseCheckerCallback = new MyLicenseCheckerCallback();
mChecker = new LicenseChecker(getApplicationContext(), new StrictPolicy(), _key_to_verify_base64);
doCheck();
Log.i("License", "Check : "+didCheck);
if(didCheck){
result.success(licensed);
}
else{
result.success(false);
}
}
}
}
);
}
private void doCheck() {
didCheck = false;
checkingLicense = true;
setProgressBarIndeterminateVisibility(true);
mChecker.checkAccess(mLicenseCheckerCallback);
}
private class MyLicenseCheckerCallback implements LicenseCheckerCallback {
@Override
public void allow(int reason) {
if(isFinishing()){
Log.i("License", ""+isFinishing());
return;
}
Log.i("License","Accepted!");
message = 1;
licensed = true;
checkingLicense = false;
didCheck = true;
}
@Override
public void dontAllow(int reason) {
if(isFinishing()){
Log.i("License", ""+isFinishing());
return;
}
Log.i("License", "Reason : "+reason);
message = 0;
licensed = false;
checkingLicense = false;
didCheck = true;
}
@Override
public void applicationError(int errorCode) {
Log.i("License", "Error : "+errorCode);
if(isFinishing()){
Log.i("License", ""+isFinishing());
return;
}
message = 0;
licensed = true;
checkingLicense = false;
didCheck = false;
}
}
}
Thank you for your time.