AmazfitSpringboardPluginExample icon indicating copy to clipboard operation
AmazfitSpringboardPluginExample copied to clipboard

can't get applicationcontext.

Open Klaus3d3 opened this issue 6 years ago • 3 comments

Hi,

it's not excactly an issue with the springboard but a problem.

I'm working on a watchface based on AposGreatFit with a Background Service and a springboard widget for Settings. I want to use sharedpreferences to read and write Settings from widget and from the Service. For this i need the applicationcontext. Problem is the only working appcontext i can get within the widget is from mHost.getHostWindow.getcontext().getapplicationcontext.

this Returns a different appcontext than getapplicationcontext within my Service therefor the Settings get saved in different files. The widgets context seem the implicate that the widget is not pasrt of my app but runs within the launcher app.

When i try the common solution for this with an application class wich Returns the applicationcontext i get npe when i call this from the widget. Calling this from the Service is ok.

do you know a solution?

Klaus3d3 avatar Aug 31 '18 06:08 Klaus3d3

Hi,

You are correct in saying that the plugin runs with the launcher context. For this reason, SharedPreferences are difficult to implement. As the watch is running Android 5.0, you can still use MODE_WORLD_WRITEABLE or MODE_WORLD_READABLE for the SharedPreferences, which will allow them to be accessible from a different Context (you may need to use Context.createPackageContext to get the right SharedPreferences location)

Alternatively you could write the preferences to /sdcard somewhere, using your own preference implementation.

Hope this helps!

KieronQuinn avatar Sep 01 '18 15:09 KieronQuinn

i already tried that with PackageContext and Mode-World_writeable. Did'nt get this to work, too. I always got" E/SharedPreferencesImpl: Couldn't create directory for SharedPreferences file".

Maybe i did sometning wrong there, but now i'm using Apo's SettinAPSetting wich he already implemented in the watchface. This works for me. Since it's not a phone with internet access there should be no security issues.

Klaus3d3 avatar Sep 01 '18 20:09 Klaus3d3

You'd need to create the initial shared preferences in your app, rather than the plugin if you're trying to do that. The launcher plugin won't have permissions to create a directory, only your app

KieronQuinn avatar Sep 01 '18 20:09 KieronQuinn