Xamarin/C# 中的 Android FileObserver 示例? [英] Android FileObserver example in Xamarin/C#?
问题描述
我需要在 Xamarin c# (Android) 中创建文件观察器的指导
I need guidance in creating a file observer in Xamarin c# (Android)
某种可行的例子会很棒!
Some sort of workable example would be wonderful !
我已经尝试将 java 转换为 C#,但由于我缺乏在 C# 环境中的经验,它在编译时抛出了太多错误......并且在 C# vs java 中获取编写参考代码被证明很烦人..
I've tried to convert the java ones over to C# but due to my lack on experience in the C# environment, it's throwing too many errors when being compiled .. and getting the write reference code within C# vs java is proving irritating ..
所以请!,可能有人在那里指点我一些可行的
So PLEASE !, may someone out there point me to some sort of workable
这是一个文件观察者的java例子https://gist.github.com/shirou/659180
This is a java example of a file observer https://gist.github.com/shirou/659180
推荐答案
创建一个继承自 的类Android.OS.FileObserver
,你只需要实现OnEvent()
和 one(+) 构造函数.在你看过一次之后,它是一个非常简单的模式...... ;-)
Create a class that inherits from Android.OS.FileObserver
, you only need to implement the OnEvent()
and one(+) Constructors. Its a really simple pattern after you see it once... ;-)
注意事项:
- 在路径上观看,如果您需要按文件过滤,请在 OnEvent 中进行
- 不要让你的 FileObserver 对象被垃圾回收,否则你的 OnEvents 会神奇地停止 :-/
- 记得调用 StartWatching() 以接收 OnEvent 调用
- Watch on a path, if you need to filter by file, do it in the OnEvent
- Do not let your FileObserver object get GC'd or your OnEvents will magically stop :-/
- Remember to call StartWatching() in order to receive OnEvent calls
FileObserver 类:
using System;
using Android.OS;
using Android.Util;
namespace MyFileObserver
{
public class MyPathObserver : Android.OS.FileObserver
{
static FileObserverEvents _Events = (FileObserverEvents.AllEvents);
const string tag = "StackoverFlow";
public MyPathObserver (String rootPath) : base(rootPath, _Events)
{
Log.Info(tag, String.Format("Watching : {0}", rootPath));
}
public MyPathObserver (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));
}
}
}
示例用法:
var pathToWatch = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);
// Do not let myFileObserver get GC'd, stash it's ref in an activty, or ...
myFileObserver = new MyPathObserver (pathToWatch);
myFileObserver.StartWatching (); // and StopWatching () when you are done...
var document = Path.Combine(pathToWatch, "StackOverFlow.txt");
button.Click += delegate {
if (File.Exists (document)) {
button.Text = "Delete File";
File.Delete (document);
} else {
button.Text = "Create File";
File.WriteAllText (document, "Foobar");
}
};
adb logcat 输出(点击测试按钮时):
I/StackoverFlow( 3596): StackOverFlow.txt:Create
I/StackoverFlow( 3596): StackOverFlow.txt:Open
I/StackoverFlow( 3596): StackOverFlow.txt:Modify
I/StackoverFlow( 3596): StackOverFlow.txt:CloseWrite
I/StackoverFlow( 3596): StackOverFlow.txt:Delete
I/StackoverFlow( 3596): StackOverFlow.txt:Create
I/StackoverFlow( 3596): StackOverFlow.txt:Open
I/StackoverFlow( 3596): StackOverFlow.txt:Modify
I/StackoverFlow( 3596): StackOverFlow.txt:CloseWrite
I/StackoverFlow( 3596): StackOverFlow.txt:Delete
I/StackoverFlow( 3596): StackOverFlow.txt:Create
I/StackoverFlow( 3596): StackOverFlow.txt:Open
I/StackoverFlow( 3596): StackOverFlow.txt:Modify
I/StackoverFlow( 3596): StackOverFlow.txt:CloseWrite
这篇关于Xamarin/C# 中的 Android FileObserver 示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!