1

I would like my application after making it five calculations. That is, using 5 times. After using it to block five times, and would show the message to buy the PRO version

How does my application I calculate, calculate and use the button, I guess I could count how many times the compute button is pressed, just do not know how to lock after 5 times.

Someone help me, thanks

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
GDawson
  • 337
  • 1
  • 7
  • 19

4 Answers4

3

Create a Class that keeps track of your app loads and saves them in the SharePreferences

public static Boolean app_launched(Context mContext) {
            SharedPreferences prefs = mContext.getSharedPreferences("loadCounter", 0);
            SharedPreferences.Editor editor = prefs.edit();

            // Increment launch counter
            long launch_count = prefs.getLong("launch_count", 0) + 1;
            editor.putLong("launch_count", launch_count);
            editor.commit();

            if(launch_count > 5)
                return false;

            return true;
}

call that function in the onCreate of your Start Activity.

Woodsy
  • 3,177
  • 2
  • 26
  • 50
  • That's what I was about to suggest! You beat me to it ;) – Jean-Philippe Roy Mar 12 '12 at 19:45
  • I use something similar to this to display a 'Rate My App' dialog after x loads and y days after install. Works great so far. – Woodsy Mar 12 '12 at 19:46
  • @Woodsy And if the user clears the app data from `Settings -> Applications -> Manage applications -> The APP -> Clear data` wouldn't the preferences be deleted and the user could use the app again? – user Mar 12 '12 at 19:51
  • Definitely unless you also figure out how long since they've installed it. But then they can just uninstall/reinstall the app. That's why I added my answer to this list as the correct answer really depends on how badly this needs to be enforced – BoredAndroidDeveloper Mar 12 '12 at 19:54
  • @slukian Yes you are correct. The information can be stored on the SD card instead and this would prevent it from being wiped on reinstall or Clear data but would require extra permissions. http://stackoverflow.com/questions/7085516/shared-preference-and-clear-history-data – Woodsy Mar 12 '12 at 19:58
  • I think it would not be necessary to do via the web server at the moment. Over how best to do? And avoid at least that even if the User clean the list, or uninstall and reinstall? – GDawson Mar 12 '12 at 20:35
  • @GDawson instead of returning false in the if statement, write a file to the sdcard (http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder) then replace the final `return true;` above with `return !f.exists();` (http://stackoverflow.com/questions/2625419/check-if-directory-exist-on-androids-sdcard) Once that file is written to the SDCard it will not be erased on reinstall or when they wipe the data. – Woodsy Mar 13 '12 at 12:21
  • 2
    You can still delete the file on the SD card though. Like I said, it depends on how badly you need to limit the user to only 5 uses. If you just want to make it annoying but still possible to use after 5 uses, then use Woodsy's answers. If you want to make it nearly impossible, have them use a username that logs onto a webserver OR use dcanh121's suggestion and store it in the cloud. BUT even if you store the number of uses in the cloud.. what is to stop the user from decompiling your app and just removing the line where it checks the server? That's why you need obfuscation. – BoredAndroidDeveloper Mar 13 '12 at 14:43
  • Although I should mention that even obfuscation wont really prevent someone who is determined. The only true way to accomplish a trial is to only include limited functionality so there is nothing to decompile. If you have a game.. only include one level. Or only let them unlock one gun. There are ways around just limiting them to 5 plays – BoredAndroidDeveloper Mar 13 '12 at 14:47
2

The key here is that someone can always take your app, decompile it, then change it so that it bypasses the count OR just figure out how you're storing the count and then revert it to the initial state.

So really this depends on how badly you need to enforce the 5 times only rule.

If you want to make it annoying.. but not impossible, then just store in a database or file somewhere the number of times the user has used the app.

If you NEED to enforce this and make it impossible to subvert then you need to have a webserver do all of the processing and have their username only allowed 5 uses.

Remember that unless you use a webserver it is possible to get around whatever limit you do. If you can't use a webserver then you'll at least want to obfuscate your code. See these links:

BoredAndroidDeveloper
  • 1,251
  • 1
  • 11
  • 26
  • I think it would not be necessary to do via the web server at the moment. Over how best to do? And avoid at least that even if the User clean the list, or uninstall and reinstall? – GDawson Mar 12 '12 at 20:35
0

If your app connects to the internet, then without adding special permissions, you can use the Google cloud to store the device preferences.

http://code.google.com/appengine/
dcanh121
  • 4,665
  • 11
  • 37
  • 84
-1

You can check it in the OnClickListener:

button.setOnClickListener(new View.OnClickListener() {
    int counter = 0;
    @Override
    public void onClick(View v) {
        if (counter >= 5) {
            v.setEnabled(false);
            v.setClickable(false);
            // display some message
        }
        else {
            // normal operations
            ++i;
        }
    }
}

You might want counter to be static or something else in order to prevent 5 more calculations after activity change or stuff like this.

MByD
  • 135,866
  • 28
  • 264
  • 277