Forum Xamarin.Android

Persistent (Always Running) Application - Loading and Reading Files

IgorGuerraIgorGuerra BRMember
edited July 2013 in Xamarin.Android

I'm writting an application that will run solo on the Android device. That application must be always on top and always running. I'm facing some issues that I really need some help:

1 - Run after boot
The first idea was to use the "Broadcast Receiver". It works fine, but will it be called after the application is automatically updated by the Play Store?
The second idea was based on this post. Using the application as launcher looks like the solution, but that would require the user to set up the application as the default launcher, something that is not desired.
Any other ideas?

2 - Read File on Load
As soon as the application is loaded, the application loads a XML configuration file. The problem is that sometimes the file get locked (I don't know why at happens more often when the application is loading using anyone of the mehtods listed above. If I use a Thread.Sleep, the problem seems to go away, but that's not a good solution, because that would make my application take a long time to load). The only solution then, is to manually delete the file, lose all my data and run the application again. The code I use is the one below:

public static T DownloadFile<T> (string filePath, string fileName) where T : new()
{
    XmlReader xmlReader = null;
    T returnObj = default(T);
    try {
        XmlSerializer serializer = new XmlSerializer (typeof(T));

        // Read the file
        xmlReader = XmlReader.Create (Path.Combine (filePath, fileName));

        // covert reader to object
        returnObj = (T)serializer.Deserialize (xmlReader);

    } catch {
        returnObj = default(T);
    }
    finally {
        if (xmlReader != null) {
            xmlReader.Close ();
        }
    }

    return returnObj;
}

public static void SaveFile<T> (string filePath, string fileName, T objectToSave)
{
    XmlWriter xmlWriter = null;
    try {
        XmlSerializer serializer = new XmlSerializer (typeof(T));

        xmlWriter = XmlWriter.Create (Path.Combine (filePath, fileName));

        serializer.Serialize (xmlWriter, objectToSave);

        xmlWriter.Close ();
    } catch (Exception) {
    }
    finally {
        if (xmlWriter != null) {
            xmlWriter.Close();
        }
    }
}

I tried to look everywhere for solutions and for place explaining how the BroadcastReceiver works after a apk update, but the only thing I found was this post: http://stackoverflow.com/questions/17402878/android-google-play-store-closes-app-for-update-and-not-opening-it-afterwards

Thanks for the support.

Best Regards,

Igor Guerra

Posts

  • PaulKeefePaulKeefe USMember ✭✭

    If Thread.Sleep helps then maybe you need to use thread locking on the file access for read and write.

    Something like:

    private static object syncRoot = new Object();
    
    // within your method
    lock(syncRoot){
        // Read the file
        xmlReader = XmlReader.Create (Path.Combine (filePath, fileName));
    }
    
  • IgorGuerraIgorGuerra BRMember

    Paul,

    I'll give it a try. Thanks so much.

    Regards,

    Igor Guerra

Sign In or Register to comment.