Is there any way to detect when a file is created or modified or downloaded in the android phone? I checked the available intents, but none of them are related to files. I am working on the broadcast receiver and couldnt figure out a way to detect a new file creation. Can someone please help?
use FileObserver...
public class MyFileObserver extends FileObserver {
public MyFileObserver(String path) {
super(path);
}
#Override
public void onEvent(int event, String path) {
switch (event) {
case DELETE:
// file deleted
break;
case ACCESS:
// file accessed;
break;
case MODIFY:
// file modified;
break;
case OPEN:
// file opened
break;
// see documentation for available events
default:
break;
}
}
}
sample usage:
MyFileObserver fileObserver = new MyFileObserver(Environment.getExternalStorageDirectory().getPath());
fileObserver.startWatching();
Related
I am developing an app for Quick settings Tile. I want to open a layout or a custom dialog when user click on the tile, whichever is a good option. Assume this as a custom dialog. I tried many examples, but I got no luck!
Thank you in advance.
You just have to #Override method of Tile service.
#Override
public void onClick() {
super.onClick();
Tile tile = getQsTile();
switch (tile.getState()) {
case Tile.STATE_INACTIVE:
// write code for start service or open activity according to your prefrrance
StaticUtils.closeNotificationTopPanel(this);
tile.setLabel(getString(R.string.service_running));
updateTileState(Tile.STATE_ACTIVE);
break;
case Tile.STATE_ACTIVE:
updateTileState(Tile.STATE_INACTIVE);
break;
default:
updateTileState(Tile.STATE_INACTIVE);
break;
}
}
when you Click of tile service button you have to close the notification panel below code will help for that.
public static void closeNotificationTopPanel(Context context) {
Intent closeIntent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
context.sendBroadcast(closeIntent);
}
below method will help you to change the tile state and name of a button according to state.
private void updateTileState(int state) {
Tile tile = getQsTile();
if (tile != null) {
tile.setState(state);
Icon icon = tile.getIcon();
switch (state) {
case Tile.STATE_ACTIVE:
icon.setTint(Color.WHITE);
break;
case Tile.STATE_INACTIVE:
case Tile.STATE_UNAVAILABLE:
default:
icon.setTint(Color.GRAY);
break;
}
tile.updateTile();
}
}
Try to use this link: http://wintechtutorials.com/blog/android-customize-quick-setting-tiles-7-0-nougat/
Here in this link they use default Alert dialog. Try to create custom dialog as per need. Hope this will work for you.
In Android I use the FileObserver to observe a folder and do something when a new file is created.
I noticed that the CREATE event is generated on Android 5 and Android 7 but it is NOT generated on Android 8. Any idea why?
This is the pseudocode.
public class FolderObserver extends FileObserver {
String path;
static final int mask = (FileObserver.CREATE);
public FolderObserver(String path) {
super(path,mask);
this.path = path;
}
void doSomeAction(string file){}
#Override
public void onEvent(int i, #Nullable String s) {
switch (i) {
case FileObserver.CREATE:
// this event is generated on Android 5 and 7
// but is NOT generated on Android 8
doSomeAction(s);
break;
default:
break;
}
}
}
....
/*
in my client
*/
FolderObserver observer = new FolderObserver(myPath);
observer.startWatching();
Finally I found a workaround.
Copying a new file into the observed folder generates the CREATE event on Android 5 and 7 while it generates the MOVE_TO event on Android 8, strange but ok.
So I simply listen for both events like that
static final int mask = (FileObserver.CREATE | FileObserver.MOVED_TO);
....
public void onEvent(int i, #Nullable String s) {
switch (i) {
case FileObserver.CREATE:
case FileObserver.MOVED_TO:
doSomeAction(s);
break;
default:
break;
}
}
I made a project in Android Studio with Google Cloud Platform Java sample project from github a year ago. It was working correctly by then but a month ago when I opened the project, I faced the following error:
error: an enum switch case label must be the unqualified name of an enumeration constant
I have checked other solutions on SO,Ex, but on the StreamingRecognizeResponse class I saw the code is written as the solution suggests. Ex:
protected final Object dynamicMethod(
com.google.protobuf.GeneratedMessageLite.MethodToInvoke method,
Object arg0, Object arg1) {
switch (method) {
case NEW_MUTABLE_INSTANCE: {
return new com.google.cloud.speech.v1.StreamingRecognizeResponse();
}
case IS_INITIALIZED: { //**clicking on first error takes to this line.** but it is as solution suggests.
return DEFAULT_INSTANCE;
}
case MAKE_IMMUTABLE: {
results_.makeImmutable();
return null;
}
case NEW_BUILDER: {
return new Builder();
}
case VISIT: {...
...................
I can't compile my project anymore and I need to fix this error.
Any suggestion to solve this problem would help.
It is unclear which Java sample you are referring to, but com.google.protobuf enumeration GeneratedMessageLite.MethodToInvoke generally has these constants:
Object dynamicMethod(MethodToInvoke method, Object arg0, Object arg1) {
switch (method) {
case GET_DEFAULT_INSTANCE: {
return DEFAULT_INSTANCE;
}
case GET_PARSER: {
break;
}
case IS_INITIALIZED: {
break;
}
case MAKE_IMMUTABLE: {
results_.makeImmutable();
return null;
}
case MERGE_FROM: {
break;
}
case NEW_BUILDER: {
return new Builder();
}
case NEW_INSTANCE: {
return new com.google.cloud.speech.v1.StreamingRecognizeResponse();
}
case PARSE_PARTIAL_FROM: {
break;
}
}
}
There is no NEW_MUTABLE_INSTANCE and there is no VISIT. One can also list them:
for (GeneratedMessageLite.MethodToInvoke c : GeneratedMessageLite.MethodToInvoke.values()) {
System.out.println(c);
}
Also see StreamingRecognizeResponse.
I want to mount .obb file from external storage. I wrote these codes.
storageManager.mountObb(obbPath, key, new OnObbStateChangeListener() {
#Override
public void onObbStateChange(String path, int state) {
switch (state) {
case ERROR_ALREADY_MOUNTED:
storageManager.unmountObb(rawPath, true, this);
break;
case UNMOUNTED:
storageManager.mountObb(rawPath, key, this);
break;
case MOUNTED:
File mountedDir = new File(storageManager.getMountedObbPath(path));
// do something with mountedDir
break;
case ERROR_COULD_NOT_MOUNT:
case ERROR_INTERNAL:
case ERROR_PERMISSION_DENIED:
// Error occurred!!
break;
}
}
});
Now I execute this, my OnObbStateChangeListener gets state = ERROR_INTERNAL (20).
What is this error code? How to fix this?
Addition: I found this post: What causes jobb tool to throw FAT Full IOException?
Probably this is an answer. My obb file is broken.
thank you.
It looks like this constant is set in the MountService class (package com.android.server). You can take a look at the source code here : http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/com/android/server/MountService.java but as you can see, there are various reasons for setting the state at ERROR_INTERNAL
I need to read all files in a directory every say 5 minutes and check if any new files have been created.
What would be the best way to do this?
I thought of using a DB to store all file names and then compare one by one but it seems like it would take too long.
I also thought of writing the file names to a text file and the looking if the file name is on the list or not.
Is there a better way?
Why don't you try with FileObservers
private final class DirectoryObserver extends FileObserver {
private DirectoryObserver(String path, int mask) {
super(path, mask);
}
#Override
public void onEvent(int event, String pathString) {
event &= FileObserver.ALL_EVENTS;
switch (event) {
case FileObserver.DELETE_SELF:
//do stuff
break;
case FileObserver.CREATE:
case FileObserver.DELETE:
//do stuff
break;
}
}
}
Ref:FileObserver CREATE or DELETE received only for files