【问题标题】:Android FileObserver example in Xamarin/C#? [closed]Xamarin/C# 中的 Android FileObserver 示例? [关闭]
【发布时间】:2016-02-15 15:31:24
【问题描述】:

我需要在 Xamarin c# (Android) 中创建文件观察器的指导

一些可行的例子会很棒!

我尝试将 java 转换为 C#,但由于我缺乏 C# 环境经验,它在编译时抛出了太多错误......并且在 C# vs java 中获取写入参考代码被证明是令人恼火的..

所以请!,有人可以指点我一些可行的方法

这是一个文件观察器的java示例 https://gist.github.com/shirou/659180

【问题讨论】:

  • 你的意思是文件资源管理器?
  • 不,一个 Fileobserver .. 来查看文件何时被访问或修改
  • 我添加了一个java示例
  • 到底是什么问题?该示例似乎是从 Android FileObserver 继承的,并且没有做太多其他事情。

标签: c# android file xamarin observers


【解决方案1】:

创建一个继承自Android.OS.FileObserver的类,你只需要实现OnEvent()和一个(+)的构造函数。看过一次之后,它是一个非常简单的模式... ;-)

注意事项:

  • 路径上观看,如果需要按文件过滤,请在 OnEvent 中进行
  • 不要让您的 FileObserver 对象被 GC 处理,否则您的 OnEvents 将神奇地停止:-/
  • 记得调用 StartWatching() 以接收 OnEvent 调用

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

【讨论】:

  • 1 个问题 - 什么是“不要让您的 FileObserver 对象被 GC”
  • 不要让它获得“垃圾收集”,将其定义为类级别的变量(如在活动中),就好像将其分配给本地方法变量一样,它将超出范围并且它可以被 GC 处理,因为它在任何地方都没有引用......
  • 谢谢罗伯特,你是一个帮助我的好心人..不像那些搁置这个问题的人..
猜你喜欢
  • 2012-08-14
  • 2012-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多