4

我已经在三部手机上试过了,但我的 FileObserver 只能在 Android 4.2.2 上运行的手机上工作,它无法捕获其他版本上的任何事件。这是我的代码:在 AndroidMenifest 中,我已经添加了使用权限:

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

在活动中,像这样:

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();
}
4

0 回答 0