这个问题是this问题的延伸。发布另一个问题因为关闭了。
我正在实现@SushiHangover所提到的FileObserver类,但是当事件( OnEvent )被触发时,path的值为null,e的值为1073741856。
这就是我按照@SushiHangover
的建议初始化FileObserver的方法[Activity(Label = "Main Menu")]
public class MainView : MvxActivity
{
public MusicFolderObserver MusicFolderObserver;
protected override void OnViewModelSet()
{
File musicFolder = new File(Environment.GetExternalStoragePublicDirectory(Environment.DirectoryMusic), "/MyFolder");
MusicFolderObserver = new MusicFolderObserver (musicFolder.AbsolutePath);
MusicFolderObserver.StartWatching();
SetContentView(Resource.Layout.View_Main);
}
}
以下是文件观察者类
public class MusicFolderObserver : FileObserver
{
static FileObserverEvents _Events = (FileObserverEvents.AllEvents);
const string tag = "StackoverFlow";
public MusicFolderObserver (String rootPath) : base(rootPath, _Events)
{
Log.Info(tag, String.Format("Watching : {0}", rootPath));
}
public MusicFolderObserver (String rootPath, FileObserverEvents events) : base(rootPath, events)
{
Log.Info(tag, String.Format("Watching : {0} : {1}", rootPath, events));
}
public override void OnEvent(FileObserverEvents e, String path)
{
Log.Info(tag, String.Format("{0}:{1}", path, e));
}
}
请让我知道如何在应用运行的整个过程中让这个观察者处于活动状态。我正在尝试制作一个全天候活动的应用程序,并在观察到的音乐文件夹
中发生任何变化时触发答案 0 :(得分:0)
1073741856
的FileObserverEvents.Open
包含一个if (e.HasFlag(FileObserverEvents.Open))
{
Log.Debug(tag, $"File Opened : {path}");
}
标记:
FileObserverEvents
当Android(Linux)操作目录中的文件时,您可能会在更改目录内容时收到一系列事件,但很多这些操作(OnEvent
)都没有附加到它们的路径,在您的情况下可以忽略这些(如果您在获得空路径时需要所有事件标志的详细信息,请参阅Linux文件系统参考)。
public override void OnEvent(FileObserverEvents e, String path)
{
if (path == null) return;
Log.Debug(tag, $"{path} : {e}");
}
覆盖示例:Touch
adb shell touch /sdcard/Music/MyFolder/BohemianRhapsody100.mp3
新文件:SO : BohemianRhapsody100.mp3 : Create
SO : BohemianRhapsody100.mp3 : Open
SO : BohemianRhapsody100.mp3 : CloseNowrite
结果是:
adb shell mv /sdcard/Music/MyFolder/BohemianRhapsody100.mp3 /sdcard/Music/MyFolder/BohemianRhapsody101.mp3
SO : BohemianRhapsody100.mp3 : MovedFrom
SO : BohemianRhapsody101.mp3 : MovedTo
结果是:
adb shell rm /sdcard/Music/MyFolder/BohemianRhapsody101.mp3
SO : BohemianRhapsody101.mp3 : Delete
结果是:
$_SESSION['username']
等...