Could I access the Property of Xamarin.Forms.Application.Current in Android Service?

xinmengxinmeng GBMember ✭✭

The scenario is that, I start service on Android to run background( I close app thoroughly by swipe it out in the task manager). In this service, I want to access the Property Dictionary of Xamarin.Forms.

Currently, I try to get it, and return null.

So is there any solution to access it? Or I need to change other key-value storage solution which can be access in the background?

Answers

  • Kan_nanKan_nan INMember ✭✭

    same issue here.. :)

  • Kan_nanKan_nan INMember ✭✭

    After few search, I understood that it is not possible that way(from background service). You've to use PreferenceManager provided to store persistant data in Xamarin Android.
    You can sync your data stored in your Xamarin Application.Current dictionary to PreferenceManager store.

    How to do this??

    From your "MainActivity.cs", after loading xamarin.form application, initialize "ISharedPreferences" interface object with "PreferenceManager.GetDefaultSharedPreferences(Context);" .Like...

    PreferenceManager.GetDefaultSharedPreferences(Context);

    From "ISharedPreferences" object , you can initialize "ISharedPreferencesEditor" interface object like,
    sharedPrefs.Edit(); where "sharedPrefs" is ISharedPreferences object;

    ISharedPreferencesEditor have set of methods to Put and Read data from and to the store...

    Hope this will help some one :)

  • NamyslawSzymaniukNamyslawSzymaniuk USMember ✭✭✭✭

    First of all @Kan_nan do you really need Android SharedPreferences ? Couldn't you just simply use this cross-platform lib, that using SharedPreferences for Android implementation instead https://github.com/jamesmontemagno/SettingsPlugin

  • Kan_nanKan_nan INMember ✭✭

    @NamyslawSzymaniuk said:
    First of all @Kan_nan do you really need Android SharedPreferences ? Couldn't you just simply use this cross-platform lib, that using SharedPreferences for Android implementation instead https://github.com/jamesmontemagno/SettingsPlugin

    Wow... Thanks. Useful information. :)

Sign In or Register to comment.