0

当文件插入 sdcard 时,我正在尝试开始一项活动。为此,我想启动一个 helloworld.java 活动(虚拟活动)。我在 startActivity() 方法中收到“未定义”错误。Eclipse 用红色下划线突出显示错误代码代码。我已经在清单文件中注册了这两个类。所以清单文件没有问题。

    public class MyFileObserver extends FileObserver {
    public static final String PREFS_NAME = "MyPreferencesFile";
    public static String absolutePath;
    //final adapter info = new adapter(this);
      HashSet<ObserverActivity> registeredObservers;
    FileEvent fileevent = new FileEvent();
    final filehelper f_help = new filehelper(fileevent);
    private Context context;

      public MyFileObserver(Context context) {
          super(absolutePath);
             this.context = context;
         }

    public MyFileObserver(String path) {
        super(path, FileObserver.ALL_EVENTS);
        //this.fileevent = fileevent;
        absolutePath = path;
        registeredObservers = new HashSet<ObserverActivity>();

    }
    public void registerObserver(ObserverActivity toRegister){
          registeredObservers.add(toRegister);
        }

        public void unregisterObserver(ObserverActivity toUnregister){

        registeredObservers.remove(toUnregister);
        }

    @Override
    public void onEvent(int event, String path) {
    //  try{

        if (path == null)
        {
            return;
        }

        /*for(ObserverActivity o: registeredObservers){
            o.onFileObserved(event, path);
          }*/

        //a new file or subdirectory was created under the monitored directory
        if ((FileObserver.CREATE & event)!=0) {
            FileAccessLogStatic.accessLogMsg += absolutePath + "/" + path + " is created\n";
            Log.v(path+ " in FileObserver of sample_fileobserver ====>>>> ",path); 
        //  setpath(path);
            //fileevent.insert(path);
            /*for(ObserverActivity o: registeredObservers){
                o.onFileObserved(event, path);
              }*///try
            //  {
            FileEvent.path2 = path;
            Intent i = new Intent("com.example.sample_fileobserver.hello");
            startActivity(i);

        //  startAct();
                //  fileevent.insert(path);
            //  }
            //catch(Exception e) 
            //{
            //  Log.v("Activity cannot be started ====>>>> ",e.toString());
        //  }

            //Intent i=new Intent("com.example.seperate_fileobserver.FileEvent");
          //  i.putExtra("path", path);
         //   startActivity(i);


        }
        //a file or directory was opened
        if ((FileObserver.OPEN & event)!=0) {
            FileAccessLogStatic.accessLogMsg += path + " is opened\n";
        }
        //data was read from a file
        if ((FileObserver.ACCESS & event)!=0) {
            FileAccessLogStatic.accessLogMsg += absolutePath + "/" + path + " is accessed/read\n";

        }
        //data was written to a file
        if ((FileObserver.MODIFY & event)!=0) {
            FileAccessLogStatic.accessLogMsg += absolutePath + "/" + path + " is modified\n";

        }
        //someone has a file or directory open read-only, and closed it
        if ((FileObserver.CLOSE_NOWRITE & event)!=0) {
            FileAccessLogStatic.accessLogMsg += path + " is closed\n";
        }
        //someone has a file or directory open for writing, and closed it 
        if ((FileObserver.CLOSE_WRITE & event)!=0) {
            String filename = "";
            int numbers = 0;
            f_help.insertpic(filename,numbers);
            FileAccessLogStatic.accessLogMsg += absolutePath + "/" + path + " is written and closed\n";
        }
        //[todo: consider combine this one with one below]
        //a file was deleted from the monitored directory
        if ((FileObserver.DELETE & event)!=0) {
            //for testing copy file
//          FileUtils.copyFile(absolutePath + "/" + path);
            FileAccessLogStatic.accessLogMsg += absolutePath + "/" + path + " is deleted\n";
            Log.v("deleting path",path);
        //  fileevent.delete(path);
            //for(ObserverActivity o: registeredObservers){
             //   o.onFileObserved(event, path);
            //  }
            try{
            fileevent.delete(path);
            }
            catch(Exception e)
            {
                Log.v("File cannot be deleted ====>>>> ",e.toString());
            }
        }
        //the monitored file or directory was deleted, monitoring effectively stops
        if ((FileObserver.DELETE_SELF & event)!=0) {
            FileAccessLogStatic.accessLogMsg += absolutePath + "/" + " is deleted\n";
        }
        //a file or subdirectory was moved from the monitored directory
        if ((FileObserver.MOVED_FROM & event)!=0) {
            FileAccessLogStatic.accessLogMsg += absolutePath + "/" + path + " is moved to somewhere " + "\n";
        }
        //a file or subdirectory was moved to the monitored directory
        if ((FileObserver.MOVED_TO & event)!=0) {
            FileAccessLogStatic.accessLogMsg += "File is moved to " + absolutePath + "/" + path + "\n";
        }
        //the monitored file or directory was moved; monitoring continues
        if ((FileObserver.MOVE_SELF & event)!=0) {
            FileAccessLogStatic.accessLogMsg += path + " is moved\n";
        }
        //Metadata (permissions, owner, timestamp) was changed explicitly
        if ((FileObserver.ATTRIB & event)!=0) {
            FileAccessLogStatic.accessLogMsg += absolutePath + "/" + path + " is changed (permissions, owner, timestamp)\n";
        }

在此处输入图像描述

我无法理解为什么它未定义,以及为什么 FileObserver.onEvent() 不支持 startActivity(Intent) 方法。

提前致谢。

4

2 回答 2

0

试试context.startActivity(i)吧。

于 2013-09-28T16:16:11.817 回答
0

startActivity 是 Context 的方法和 Context 的子类,例如 Activity 和 Service。您可能可以使用context.startActivity(intent)来消除语法错误,但您可能需要考虑将此功能放在服务中。

于 2013-09-28T16:17:13.313 回答