4

I have tried it on three phones, but my FileObserver can only work on a phone that works on Android 4.2.2, and it cannot catch any event on other versions.Here is my code: in AndroidMenifest I already added uses-permissions:

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

in activity, like this:

static class DownloadObserver extends FileObserver {
        public DownloadObserver(String path) {
            super(path);
        }

    @Override
    public void onEvent(int event, String path) {
        switch (event) {
            case FileObserver.MODIFY:
                Log.e(TAG, ":MODIFY");
                break;
            case FileObserver.ACCESS:
                Log.e(TAG, ":ACCESS");
                break;
            case FileObserver.CREATE:
                Log.e(TAG, ":CREATE");
                break;
            default:
                Log.e(TAG, ":default");
                break;
        }
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_download);
    request = new DownloadManager.Request(Uri.parse(PACKAGE_URL))
        .setAllowedOverRoaming(false)
        .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI)
        .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
        .setVisibleInDownloadsUi(true)
        .setDestinationInExternalPublicDir("/download/", getVersionName() + ".apk");

    manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), getVersionName() + ".apk");
    Log.e(TAG, "file.getParent() = " + file.getParent());
    observer = new DownloadObserver(file.getParent());
    observer.startWatching();
    mTaskId = manager.enqueue(request);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    observer2.stopWatching();
}
KevinYe
  • 61
  • 4

0 Answers0