Home FileObserver callback won't trigger (Related topics on stackoverflow mentioned in details)
Reply: 0

FileObserver callback won't trigger (Related topics on stackoverflow mentioned in details)

user3278
1#
user3278 Published in April 23, 2018, 9:37 am

I'm trying to listen to any event in external storage directory, i.e. file added, removed or modified through Android FileObserver but its onEvent method never gets called.

I've tried all of the following solutions:

Android FileObserver [duplicate]

FileObserver instance is being garbage collected

How do you implement a FileObserver from an Android Service

And as for my code, its given below:

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.grapgame.fileobserverdemo">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
    android:name=".AppClass"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".FileObserverService" />
</application>

</manifest>

Service

public class FileObserverService extends Service {

public static final String TAG = "FileObserverService";

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "onStart");

    final String pathToWatch = android.os.Environment.getExternalStorageDirectory().toString();
    Toast.makeText(this, "My Service Started and trying to watch " + pathToWatch, Toast.LENGTH_LONG).show();

    AppClass.fileObserver = new FileObserver(pathToWatch) { // set up a file observer to watch this directory on sd card
        @Override
        public void onEvent(int event, String file) {
            Log.d(TAG, "File created [" + pathToWatch + file + "]");
        }
    };
    AppClass.fileObserver.startWatching();
    return START_STICKY;
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

AppClass.java

public class AppClass extends Application {
    public static FileObserver fileObserver;

    @Override
    public void onCreate() {
        super.onCreate();
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent(this, FileObserverService.class);
        startService(intent);
    }
}

I've also tried it without AppClass.java, I found this solution on Stackoverflow and tried. I also tried this GitHub code but nothing seems to work.

I know FileObserver is not recursive, for now I'm just trying to listen to one directory.

Any help is highly appreciated.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.435071 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO