Search code examples
androidandroid-intentandroid-servicealarmmanagerintentservice

Android: IntentService. Where to save files?


This question was edited after I saw the comment of ianhanniballake. I removed the irrelevant parts.

I have an intent service that repeatedly called by AlarmManager, and reads some file (the content of the file changes rarely) to extract PublicKey object.

What is the best place to store a file (that periodically accessed)? What is the fastest memory? I guess it is SharedPreferences, InternalStorage and in the end ExternalStorage, but I didn't find an answer for this question.

Is there any technique to make such a procedure efficiently (without reading the file everytime)?


Solution

  • I am not sure what you are trying to achieve in the big picture. But i will take a stab at answering your much more targeted questions.

    1. For storing a file, you don't really have much choices - it's going to be internal or external file system and/or on the cloud. If you want to store data in a non-file format, your choices will expand to include built in SQLite database and SharedPreferences (persistent)
    2. I don't see what you can't use SharedPreferences or Internal Storage with IntentService. The IntentService and the remaining part of your application are all running under the same Linux User ID i.e process. So, you should be able to use SharedPreferences or Internal Storage as well.
    3. IntentService extends Service and creates a worker thread for you automatically. It makes managing the thread easy for you. If you just use Service instead, you have to create and manage the worker thread on your own. So, IntentService is a bit more convenient to use. The only downside of using IntentService is that you can't handle multiple requests at the same time, it queues the requests and handles one at a time. But with Service, you can write code to handle multiple requests as you wish. When you are using a Service for RPC (inter-process), it's best not to use IntentService and stick to plain Service.

    HTH.