我正在尝试制作一个可以对屏幕截图文件夹做出反应并为开始干杯的应用程序,我是一名新开发人员,这是我第一次使用文件观察器,所以我只能猜测我犯了很多错误。问题是截图时没有吐司或登录。这是我的观察者类中的代码:
public class listeningInit extends FileObserver {
private static final String TAG = "File listener";
public String absolutePath;
public listeningInit(String path) {
super(path, FileObserver.ALL_EVENTS);
absolutePath = path;
}
@Override
public void onEvent(int event, String path) {
if ((FileObserver.CREATE & event)!=0) {
Log.v(TAG, absolutePath + "/" + path + " is created\n");
Context context = getContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "Folder action!", duration);
toast.show();
}
}
private Context getContext() {
// TODO Auto-generated method stub
return null;
}
这是执行 .startWatching 的活动的代码
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listeningInit startObs = new listeningInit("/Pictures/Screenshots/");
startObs.startWatching();
//Checking if this is a first run
Boolean firstRun = false;
SharedPreferences run = getSharedPreferences("MYPREFS", 0);
firstRun = run.getBoolean("fr", true);
//if true launch tutorial activity
if(firstRun == true){
Intent k = new Intent(MainActivity.this, Tutorial.class);
startActivity(k);
}
}
代码没有抛出任何错误,只是没有响应,我不知道为什么会这样。