Reputation: 886
I have been writing a Dropbox like android app, which automatically upload photos to the server. When user take photos by camera and save them on SD Card, it will be noticed to upload these photos.
I started a service to ensure long time listening, and registered a Content Observer to listen camera event. But when OS under resource pressure, it will kill the service and the Content Observer wont be fire any more. So how to make the Content Observer always working?
BTW, the point of my question is asking how to keep Content Observer running as long as possible just like service does. And if Content Observer is not the best option what else is.
Upvotes: 0
Views: 1118
Reputation: 3993
Try this but it has some limitations:
<receiver android:name=".pictureReceiver" >
<intent-filter android:priority="10000" >
<action android:name="android.intent.action.CAMERA_BUTTON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
BroadcastReceiver implemented that's currently listening for "android.intent.action.CAMERA_BUTTON"
. However, this doesn't seem to get called when wanting it to. It only gets broadcast if the foreground activity does not consume the event.
OR
You can use FileObserver:
new FileObserver(path) {
@Override
public void onEvent(int event, String path) {
if (event == FileObserver.CREATE)
Toast.makeText(getActivity(), "File created", Toast.LENGTH_SHORT).show();
}
}.startWatching();
Upvotes: 1