Android MyFileObserver类型的startActivity(Intent)方法未定义
当文件插入SD卡时,我正在尝试启动一个活动。为此,我想启动一个helloworld.java活动(虚拟活动)。我在startActivity()方法中遇到“未定义”错误。Eclipse用红色下划线突出显示错误代码。我已经在清单文件中注册了这两个类。所以清单文件中没有问题Android MyFileObserver类型的startActivity(Intent)方法未定义,android,Android,当文件插入SD卡时,我正在尝试启动一个活动。为此,我想启动一个helloworld.java活动(虚拟活动)。我在startActivity()方法中遇到“未定义”错误。Eclipse用红色下划线突出显示错误代码。我已经在清单文件中注册了这两个类。所以清单文件中没有问题 public class MyFileObserver extends FileObserver { public static final String PREFS_NAME = "MyPreferencesF
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";
}
公共类MyFileObserver扩展FileObserver{
公共静态最终字符串PREFS_NAME=“MyPreferencesFile”;
公共静态字符串绝对路径;
//最终适配器信息=新适配器(此);
HashSet注册服务器;
FileEvent FileEvent=newfileevent();
final filehelper f_help=新filehelper(fileevent);
私人语境;
公共MyFileObserver(上下文){
超级(绝对路径);
this.context=上下文;
}
公共MyFileObserver(字符串路径){
super(路径、FileObserver.ALL_事件);
//this.fileevent=fileevent;
绝对路径=路径;
registeredObservers=newhashset();
}
public void registerObserver(ObserverActivity-to-register){
registeredObservers.add(重新注册);
}
public void Unregister Observer(ObserverActivity toUnregister){
registeredObservers.remove(toUnregister);
}
@凌驾
public void onEvent(int事件,字符串路径){
//试一试{
if(路径==null)
{
返回;
}
/*for(observer活动o:Registeredobserver){
o、 onFileObserved(事件、路径);
}*/
//在受监视的目录下创建了一个新文件或子目录
如果((FileObserver.CREATE&event)!=0){
FileAccessLogStatic.accessLogMsg+=absolutePath+“/”+path+“已创建\n”;
Log.v(示例的FileObserver中的path+”\u FileObserver====>>>>”,path);
//设置路径(路径);
//fileevent.insert(路径);
/*for(observer活动o:Registeredobserver){
o、 onFileObserved(事件、路径);
}*///试一试
// {
FileEvent.path2=路径;
Intent i=newintent(“com.example.sample_fileobserver.hello”);
星触觉(i);
//startAct();
//fileevent.insert(路径);
// }
//捕获(例外e)
//{
//Log.v(“无法启动活动===>>>”,例如toString());
// }
//意图i=新意图(“com.example.separate_fileobserver.FileEvent”);
//i.putExtra(“路径”,路径);
//星触觉(i);
}
//已打开文件或目录
如果((FileObserver.OPEN&event)!=0){
FileAccessLogStatic.accessLogMsg+=path+“已打开\n”;
}
//数据是从文件中读取的
如果((FileObserver.ACCESS&event)!=0){
FileAccessLogStatic.accessLogMsg+=absolutePath+“/”+path+“被访问/读取\n”;
}
//数据被写入一个文件
如果((FileObserver.MODIFY&event)!=0){
FileAccessLogStatic.accessLogMsg+=absolutePath+“/”+path+“已修改\n”;
}
//有人以只读方式打开文件或目录,然后将其关闭
如果((FileObserver.CLOSE_NOWRITE&event)!=0){
FileAccessLogStatic.accessLogMsg+=path+“已关闭\n”;
}
//有人打开了一个文件或目录进行写入,然后关闭了它
如果((FileObserver.CLOSE_WRITE&event)!=0){
字符串filename=“”;
整数=0;
f_help.insertpic(文件名、数字);
FileAccessLogStatic.accessLogMsg+=absolutePath+“/”+path+“已写入并关闭\n”;
}
/t/(todo:考虑将这一个与下面的组合)
//已从受监视的目录中删除一个文件
如果((FileObserver.DELETE&event)!=0){
//用于测试复制文件
//copyFile(绝对路径+“/”+路径);
FileAccessLogStatic.accessLogMsg+=absolutePath+“/”+path+“已删除\n”;
Log.v(“删除路径”,路径);
//fileevent.delete(路径);
//for(observer活动o:Registeredobserver){
//o.观察到的事件(事件、路径);
// }
试一试{
fileevent.delete(路径);
}
捕获(例外e)
{
Log.v(“无法删除文件==>>>”,例如toString());
}
}
//已删除受监视的文件或目录,监视将有效停止
如果((FileObserver.DELETE_SELF&event)!=0){
FileAccessLogStatic.accessLogMsg+=绝对路径+“/”+“已删除\n”;
}
//文件或子目录已从受监视的目录中移动
如果((FileObserver.MOVED_FROM&event)!=0){
FileAccessLogStatic.accessLogMsg+=absolutePath+“/”+path+”被移动到“+”\n”的某个位置;
}
//已将文件或子目录移动到受监视的目录
如果((FileObserver.MOVED_TO&event)!=0){
FileAccessLogStatic.accessLogMsg+=“文件被移动到“+绝对路径+”/“+路径+”\n”;
}
//已移动受监视的文件或目录;监视将继续
如果((FileObserver.MOVE_SELF&event)!=0){
FileAccessLogStatic.accessLogMsg+=路径+“已移动\n”;
}
//元数据(权限、所有者、时间戳)已显式更改
如果((FileObserver.ATTRIB&event)!=0){
FileAccessLogStatic.accessLogMsg+=absolutePath+“/”+path+“已更改(权限、所有者、时间戳)\n”;
}
我无法理解为什么它是未定义的,以及为什么FileObserver.onEvent()不支持startActivity(Intent)方法
提前感谢。尝试
上下文。startActivity(i)
。startActivity是上下文的一种方法