Difference between Android Service and Content Provider - android

I am developing an app and get confused about the idea of Service and Content Provider in Android. In practice, what will be the difference between them?
Content Provider
is a facade and it defines a way to share data among applications. You many attach a local database to your app or create Content Provider mapped to a universal database so that all the application on the same device can share it.
Service
is long running processes that need to be decoupled from main activity. It has local and remote service. local service is like the local database, and remote service is like Content Provider sharing the database info.
What My App is doing?
downloads info. from multiple internet resource in the background (I suppose this will be Service) and store the info. into database, and multiple applications will need to retrieve the data, format them and output them to user (I guess it will be a Content Provider).
What will be the fine line between Service and Content Provider? Newbie in Android, and any suggestion is welcome.
Lily

Your understanding of the difference between a Service and ContentProvider is pretty spot on. The key thing is that a ContentProvider simply acts as a conduit for retrieving data, while a Service is meant to do something in the background without user interaction.

Related

Syncing mehod for android chat application

I am implementing a chat app in android. A vital part of this app is to sync with the server and local database. There are several methods to sync data between server and android device like AsyncTask, IntentService and SyncAdapter.
I prefer to use SyncAdapter, because it is more efficient and it handles most of the background tasks by itself.
When I read the developer page for SyncAdapter I found this,
Note: Sync adapters run asynchronously, so you should use them with the expectation that they transfer data regularly and efficiently, but not instantaneously. If you need to do real-time data transfer, you should do it in an AsyncTask or an IntentService.
Does that means is it not good to use like chat app?
Also I need to mention a feature of SyncAdapter
Automated execution
Allows you to automate data transfer based on a variety of criteria, including data changes, elapsed time, or time of day. In addition, the system adds transfers that are unable to run to a queue, and runs them when possible.
So if it starts to sync when data changes (Since the new messages are stored in the sqlite database), I think SyncAdapter will be a good choice for Chat App.
Any Suggestions are appreciated.
Thanks.
Usually mobile app depends on backend implementation and app requirements, but generally you shouldn't use such methods for chat application, they won't give you up to date data.
I'd say when app is in background, you should use GCM for new messages notifications and when app is in foreground use something like RPC, xmpp, sockets or whatever that keeps your connection alive.

Intent vs Content Provider in android

I am new in android app development and while studying about the basic android components I got confused between intents and content provider as both are being used to send data from one application/component to another application/component . In case of intents we can send data using bundle or extras so why do we use content providers. Can someone please explain me this with a example . Also can we access database in android only using content provider and is this the sole reason why we use content providers ?
both are being used to send data from one application/component to another application/component
Not really.
There are four components in Android:
Activity
Service
BroadcastReceiver
ContentProvider
An Intent is none of those. An Intent is involved when we start an activity, start or bind to a service, or send a broadcast. However, comparing an Intent to a ContentProvider is akin to comparing a shovel with a boot, arguing that both can be used to carry dirt. While true, usually a boot is involved in carrying dirt, but the actual means of carrying dirt is handled by something else, such as a wheelbarrow.
In case of intents we can send data using bundle or extras so why do we use content providers.
We often use different tools for different circumstances. For example, you will find it rather difficult to carry water in a fishing net.
Each of the four components has a different role, particularly in relationship to inter-process communication (IPC):
An Activity drives the bulk of our user interface, including starting up activities from other apps (or having one of our activities be started by other apps)
A Service exists for longer-running operations that are logically decoupled from the user interface, including working with services that are implemented by other apps (or having other apps work with services that you publish)
A BroadcastReceiver is a publish/subscribe messaging system, to allow you to send messages to arbitrary subscribers, or to subscribe to messages from arbitrary senders, across process boundaries
A ContentProvider is for bulk data transfer, whether in the form of a database-style structure (rows and columns) or in the form of a stream, particularly for working with other apps
Also can we access database in android only using content provider
No. After all, if that were true, it would be impossible to access a database. A ContentProvider does not appear by magic. It has to be written by a programmer. If a ContentProvider could only access a database by means of a ContentProvider, we would have a problem.
is this the sole reason why we use content providers ?
No. In addition to offering a database-style API, a ContentProvider can also publish a stream. This is important for getting arbitrary data between apps, such as an email client making a PDF attachment available to a PDF viewer.
Intents are a messaging architecture for sending /receiving transactional commands and data. Content providers are an abstract interface to stored data for create,update, delete and sync operations.

content providers vs services

I'm just starting android development after many years of .net development. My begginners book doesn't make it clear what the difference is between a service and a content provider.
first it claims a service is a long running app which exposes it api other applications on the device. This is what a windows or a web servie would do on a pc
But then it states a content provider for the "Contacts" application exposes an API to other applications running on andriod so they can iteract with it. This is exactly what a service does. The both do the same thing. The both allow other apps to interact through their api. So what is the difference. Please point me in a a direction where i can read a more logical description of these two functionsl. As this appears to be just nonesense.
An Android service is something that runs without a user interface (in contrast to an Activity). Often said to be running in the background. This does not mean that it runs on a separate Thread though.
A content provider is a database abstraction layer. It implements CRUD not necessarily on top of a sql database. Most of the times it does. It is a defined interface to access the data behind it. Often recommended only for clients that are in separate processes than the data. I find it useful for inner application data access too.
Services are documented here and content providers here.

Database handling with 2 processes

I have a an application that has 2 parts.
A service which creates content.
An application that uses the content
Each of these run as different processes. The problem is that both of them share a database. And I frequently get database locked error, both when the service tries to write something and the UI is reading data. Also vice versa.
How do go about this?
The class used to access DB is a singleton class. But since both UI & the service are 2 different processes, there are 2 singletons I presume. So that doesn't help.
Even synchronise won't help I suppose, since again because of 2 different processes.
Content Providers maybe an option, but since I use complex queries to dig info, it would be really hard to use that too.
How do I get the two processes share the database.
Any cues would be greatly appreciated.
Using a content provider is one option. Another is to take a look at Berkeley DB. The BDB SQL API is SQLite compatible and the BDB lock manager allows multiple threads and/or processes to read/write to the database concurrently.
close the connection after each operation
catch the database locked error and try to reconnect after 50ms
or let the service handle the database and the activity ask the service for data
may be there is isDatabaseInUseMethod ?
You should use a content provider to funnel your database queries through one source. Inside of the content provider you can use any locking mechanisms you would like to ensure you're not having concurrent access. You may also think about using content observers to coordinate service actions with changes to the database.
The following is a great article on how locking works with SQLite on Android and what things to be aware of: http://kagii.squarespace.com/journal/2010/9/10/android-sqlite-locking.html
I would think you'll find some answers there :)

Android: Cache XML File from RSS Feed

I just started doing my first Android app which happens to be an RSS Reader. I did a bit of googling about this but haven't seen a clear reference though. What I want to do is cache my xml file (the feed file) to the sdcard so when the phone is offline, the user can still view the feed by automatically telling the application to look for it when no network is detected. What I have now is the mechanism to cache the image but I wonder how to use this for other files since it was specified only for images that converts it to Bitmap using the HashMap().
I think the preferred way is (just as Umakant Patil commented on your question) to store data in an SQLite database. You would typically write a background service which every now and then refreshes your database with the server side data. Your application will always read only from your SQLite database. Note that the service and the application will typically live their own lives, independently from each other (your application will never communicate directly with your service).
This procedure is also somewhat safer from an architectural perspective. Your application will never depend on network connectivity or timing issues due to network traffic. It will only rely on data and database access on your local device.
TIP #1:
You could pass an entry to the AlarmManager which will wake up your service at a given interval. Your service will synchronize your database with the RSS source and then kill itself (saving resources is always good :-)
There is a good example of Services in Android here: http://developer.android.com/reference/android/app/Service.html
And about Content Providers here:
http://developer.android.com/guide/topics/providers/content-providers.html
TIP #2:
Note that you don't necessarily need a content provider to communicate with your database. The content provider is good to have if you wish to "standardize" your database communication. Perhaps several components of your application need to access it or even several applications, then it's good to use an already defined "de facto standard" way of accessing the database.

Categories

Resources