Android Question Play Pass Program - Error with followLastLicensingURL

Jack Cole

Well-Known Member
Licensed User
Longtime User
I have 2 apps in the Google Play Pass program (subscription program by Google where users can get access to a catalog of apps for a monthly fee). In order to participate, I had to modify the licensing library to bring up an app store wall if their subscription has expired. All was well and good until I started targeting API level 28 instead of 27. When the app tries to run the code for lc.followLastLicensingURL, I get a crash with the following error.

main_lc_dontallow (java line: 1472)
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:974)
at android.app.ContextImpl.startActivity(ContextImpl.java:950)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:384)
at com.android.vending.licensing.LicenseChecker.followLastLicensingUrl(LicenseChecker.java:213)
at anywheresoftware.b4a.objects.LicenseCheckerWrapper.followLastLicensingURL(LicenseCheckerWrapper.java:86)
at mindware.mindgamespro.main._lc_dontallow(main.java:1472)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
at anywheresoftware.b4a.BA$2.run(BA.java:370)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6990)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
main_lc_dontallow (java line: 1472)
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:974)
at android.app.ContextImpl.startActivity(ContextImpl.java:950)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:384)
at com.android.vending.licensing.LicenseChecker.followLastLicensingUrl(LicenseChecker.java:213)
at anywheresoftware.b4a.objects.LicenseCheckerWrapper.followLastLicensingURL(LicenseCheckerWrapper.java:86)
at mindware.mindgamespro.main._lc_dontallow(main.java:1472)
at java.lang.reflect.Method.invoke(Native Method)

The code that I am having trouble with is the following:

B4X:
Sub lc_DontAllow (PolicyReason As Int)
    StateManager.SetSetting("licensed",False)
    StateManager.SaveSettings
    If Not(PolicyReason=lc.POLICY_RETRY) Then
        lc.followLastLicensingURL
        ExitApplication
    End If
End Sub

On the JAVA side, I have the following code.

B4X:
    public void followLastLicensingURL() {
        lc.followLastLicensingUrl(BA.applicationContext);
        // Call finish() on the surrounding activity to remove it from the backstack.
        //MainActivity.this.finish();
    }

Any ideas about what to try to make JAVA happy?
 

Jack Cole

Well-Known Member
Licensed User
Longtime User
I think I have been able to solve this on my own. The I used some newer code for the licensing library for the followLastLicensingURL function.

Java:
    public void followLastLicensingUrl(Context context) {
        String licensingUrl = mPolicy.getLicensingUrl();
        if (licensingUrl == null) {
            licensingUrl = "https://play.google.com/store/apps/details?id=" + context.getPackageName();
        }
        Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(licensingUrl));
        marketIntent.setPackage("com.android.vending");
        if (!(context instanceof Activity)) {
            marketIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(marketIntent);
    }
 
Upvote 0
Top