Licensing programming structure

Last Updated:

  1. xoggoth

    xoggoth Member

    I have copied the Google market licensing library into my project, copied the top level sample code in as and made all the necessary changes to the Manifest and strings. All error free and runs normally as was.

    What I can't figure out is the program structure to actually integrate the two properly. Do I set the as my main activity and start the previously main activity from within that? If so where does it go?

    This is incomprehensible even by Android standards and I can't find any clear examples on the net.

    Ta for any ideas.

  2. alostpacket

    alostpacket Over Macho Grande? VIP Member

    Can you post a little about the tutorial you are following?

    It's been awhile since I used LVL but essentialy the recommended way is to use the built in ServerPolicy example (though to prevent hacking you should modify it).

    I'm not sure how much this helps but I found implementing LVL to be a great learning experience but not really any good for my bottom line.
  3. xoggoth

    xoggoth Member

    Thanks for reply alostpacket. Basically just trying to follow the official Android guidance as here:

    Application Licensing | Android Developers

    The licensing library and sample I refer to is in the SDK market licensing component. I think the steps are all there in the documentation but, as you say, it looks like "a great learning experience" ie typically Androidy complicated. Just wondering if there was a simpler example anywhere of how to adapt their sample without having to figure out what stuff like "update the UI thread" means.

    Total nightmare as suspected. Code as instructions not valid java, like private Handler mHandler gives illegal modifer error. And not same code as sample. Which is most recent? Who knows? All you chaps who got this to work must be Java geniuses.
  4. alostpacket

    alostpacket Over Macho Grande? VIP Member

    The UI thread is basically your Activity.

    A Handler, is a way for your Activity to receive messages from other threads or services.

    Since LVL is a service that runs in the background and communicates with a different app, you need a way for the result to be sent back to your app.

    This is where LiceseCheckerCallback comes in. While you probably write this code in your Activity (as an inner class), you'll need to remember that it is going to be called by a different thread/app.

    So if two different threads try to call a method in your activity at the same time, you can get some serious problems (ie concurrency dead locks, but the name isnt important).

    Anyways this is where the Handler comes in. Each activity (aka UI thread) can define a Handler (private or public or whatever) that it can "give out" (pass a reference) to another class.

    In this case the Handler (mHandler) is being "given" to the LicenseCheckerCallback class.

    (if I remember correctly)

    Part of what might be confusing here: in the example they create an inner class inside the main Activity for the LicenseCheckerCallback.

    Because the LicenseCheckerCallback implementation in the example is an inner class, that means it can "see" the Handler (mHandler).

    And, because of this, there is no obvious "passing" of the handler to the LicenseCheckerCallback.

    Anyways, hope this helps -- I'm not sure where your stuck exactly but maybe this will at least give us a starting point as to what to try and figure out.

    And yeah, Android is very hard to learn at times. But some things make a TON of sense once you "get" them. Unfortunately the documentation that teaches that stuff is pretty bad :( But hopefully we can help :)
  5. xoggoth

    xoggoth Member

    Thanks very much. What you say does make sense, it's just the sample that doesn't. The license check is the main activity (public class MainActivity extends Activity) with its own layout which leaves the problems, how do you stop the licence layout being shown every time the app is opened and, if you start what was the main activity from within it, how to stop the back button going back to license check?

    Not quite got the license check example to accept it's licensed yet, what with all the inevitable problems, invalid preference page path, insufficient memory on device etc to search for solutions to, but as far as I can see it does nothing after licence checking. If it just showed a hello world page in its own view it would help.
  6. xoggoth

    xoggoth Member

  7. xoggoth

    xoggoth Member

    Using the sample to implement licensing in a previously copy protected app.

    In eclipse Android SDK Manager download:
    Google market licensing package
    Only the latest Google APIs
    You should now have in your specified path:
    the library: ..\android-sdks\extras\google\market-licensing\library
    the sample: ..\android-sdks\extras\google\market-licensing\sample

    In windows explorer:
    copy the library src\com folder to your src folder
    copy the sample android\market\licensing\ to your src\etc folder with your other source files
    copy sample res\layout\Main.xml to your res\layout folder with your other layout files
    rename current starting activity to
    rename to previous starting activity name
    rename Main.xml to License.xml

    In Eclipse refresh and:

    Ensure project properties build target is Android 2.2 (API Level 8) or above or the licensy stuff and install location line below will not compile. Do this BEFORE you make the changes to the Manifest file as errors will stop you doing it afterwards. Android is backward compatible so the App will work on devices running Android 1.5 or above. (assuming it did before obviously)

    In new top level java (now the sample code) change package name and public class name at top to be as previous, change setContentView( etc) to setContentView(R.layout.License), mod BAS64-PUBLIC_KEY and Salt values as per official Android blurb and replace the line displayResult in public void allow with:
    Intent i = new Intent(XXX.this, ViewMain.class);
    where XXX is your public class name.

    In AndroidManifest add/mod in appropriate places:
    <uses-permission android:name="" />
    <activity android:name=".ViewMain"></activity>
    <uses-sdk android:minSdkVersion="N" /> N MUST be 3 or above here to avoid install on devices that do not support licensing.
    If you want users to be able to move the app to the SD card, also add android:installLocation="auto" to the <manifest xmlns:android list. The device must have a build level of 2.2 or above to use this feature. The line is safe with lower versions but will have no effect.

    Pitfall! Eclipse tends to auto correct when you rename files so you may find some changes above done for you but it also makes changes you don't want. If the app starts normally whatever test response you have set Eclipse has probably changed the main activity in the Manifest to ViewMain.

    Open values\strings.xml and paste in all the strings from sample ..res\values\string.xml except for the app_name line

    In AVD manager create a new AVD and select target Google APIs. Need to use this for all license checking using the emulator. Setting up Google account and responses is clear in the official Android page so I won't repeat that but note that the gmail address used should not be one you have already used to purchase your app, that will just show Android market page with paid. Also ensure your test response is not set to "Handle normally" as, due to your previous version being copy protected, you will get a "not market managed" error.

    After uploading the new app you can use a phone with the previous version installed. First uninstall then go to Android market where it will say purchased and you can download new version.
    alostpacket likes this.

Share This Page