1

I've written a game which I intend to upload to the marketplace as a free demo, and I intend to offer a full version for a buck.

I'd like to make the download for the full version just a simple unlocker which writes a value to the SharedPreferences for the demo.

When the demo launches it reads its shared prefs and if the value is present then it runs in full mode, otherwise it runs in demo mode.

The reason for this is A) so that when people purchase the full version the download is close to instant instead of having to wait for the whole app to be downloaded again, and B) so that I don't have to update two market listings whenever I change the code for the app.

So, is it possible to alter the SharedPrefs for a package that is separate from the currently running package?

1337ingDisorder
  • 821
  • 1
  • 6
  • 17
  • 1
    possible duplicate of [Android: Retrieving shared preferences of other application](http://stackoverflow.com/questions/6030321/android-retrieving-shared-preferences-of-other-application) – Stephan Branczyk Feb 27 '15 at 11:02
  • That's a reference worth reading, but this question has a bit of a different ultimate goal. Look into sharedUserId or perhaps doing a cryptographic challenge-response from the game to what would be effectively a license Server in the unlocker. – Chris Stratton Feb 27 '15 at 12:54

1 Answers1

1

Android sharedpreferences for an application come in 4 modes.

When an application wants to write preferences,it is called by Context.getSharedPreferences (String name, int mode).

So it is possible to read the preferences.But to write it that particular app should be using MODE_WORLD_WRITEABLE which is deprecated.

Moral:You cannot alter the preferences of other applications.

And you really should not want to do it either

Droidekas
  • 3,464
  • 2
  • 26
  • 40
  • Hmmm ok, but in theory I could create a sharedPref entry for the unlocker app, and instead of using the unlocker to change a value for the demo's shared prefs I could just have the demo check to see if that value exists for the unlocker app, correct? If so would I just use something like SharedPreferences prefs = createPackageContext("com.mydomain.unlockerapp").getSharedPreferences("myPrefs", MODE_PRIVATE) ? – 1337ingDisorder Feb 27 '15 at 12:34
  • you would use `mode_world_readable` to make available your preferences in another app – Droidekas Feb 27 '15 at 12:37
  • Wouldn't I want to use mode_world_readable from within the unlocker app, while writing its pref? In the main game app would I not just use mode_private? – 1337ingDisorder Feb 27 '15 at 12:43
  • as long as it is written as world readable. and you want to get it in another app. you could use anything.. world readable is a choice. am not sure but mode private might work too – Droidekas Feb 27 '15 at 12:50
  • Hosting the preference in the unlocker app would work, but would be easily spoofed by something fake with the same name, unless it were a cryptographic item. And it would need to be a non-constant one, so the encrypted answer couldn't just be copied. Look at shared user ids. – Chris Stratton Feb 27 '15 at 12:51
  • Yeah i had intended to use crypto for the value. However I'm not even getting that far. When I try to get the context for another package I get this error: The method createPackageContext(String, int) is undefined. I'm trying Context unlockerContext = createPackageContext("com.mydomain.unlocker", Context.MODE_PRIVATE); – 1337ingDisorder Feb 27 '15 at 12:54
  • Ah nvm, just needed to call createPackageContext from within my active context.. ie, context.createPackageContext – 1337ingDisorder Feb 27 '15 at 12:56
  • btw thanks for the shared user ids pointer.. I was going to just use a hard-to-guess key, figuring if anyone wants to go to the trouble of trying to hack the unlocker or spoof it then they're 1337 enough to deserve a free copy ;) but might as well make it that much harder, so they have to be super-1337 to earn a free copy hehe – 1337ingDisorder Feb 27 '15 at 13:01