广播接收器在外部存储上进行媒体更改的正确intentfilter是什么?

问题描述 投票:0回答:1

我刚发现

ACTION_MEDIA_REMOVED

我正在寻找类似的东西:

ACTION_MEDIA_INSERTED和ACTION_MEDIA_CHANGED

android broadcastreceiver action intentfilter android-external-storage
1个回答
1
投票

您可以使用FileObserver来观察文件或目录中的任何更改。用法示例。

在清单文件中添加以下权限

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

public class MyFileObserver extends FileObserver {

    String absolutePath = "path to your directory";

    public MyFileObserver(String path) {
        super(path, FileObserver.CREATE);
        absolutePath = path;
    }

    @Override
    public void onEvent(int event, String path) {
        // this is where you will receive all event like file modified, file added...
        if (path != null) {
            Log.e("File created..");
        }
    }
}

在你的活动中

public class MainActivity extends AppCompatActivity {

    MyFileObserver myFileObserver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myFileObserver = new DirectoryFileObserver("Path of the directory");
        myFileObserver.startWatching();
    }
}

如果你想在后台观察外部存储,那么请参考这个answer

© www.soinside.com 2019 - 2024. All rights reserved.