gpt4 book ai didi

java - onEvent 未检测到我的应用程序中的任何事件

转载 作者:行者123 更新时间:2023-12-02 13:05:58 27 4
gpt4 key购买 nike

我正在尝试使用FileObserver监视手机的屏幕截图目录,我尝试了所有事件,方法onEvent id没有在任何事件上被调用。我所做的是安装应用程序后,拍了一张屏幕截图,但该方法没有被调用。即使尝试通过文件选择器和选定的相机文件夹更改目录并拍摄一张照片,onEvent 方法仍然没有任何操作。这是我的代码:主要 Activity :

package com.andi.shikharshah.fileobserver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.codekidlabs.storagechooser.StorageChooser;

import java.io.File;

public class MainActivity extends AppCompatActivity {
FileObserverr fileObserverr;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final StorageChooser chooser = new StorageChooser.Builder()
.withActivity(MainActivity.this)
.allowCustomPath(true)
.setType(StorageChooser.DIRECTORY_CHOOSER)
.withFragmentManager(getSupportFragmentManager())
.withMemoryBar(true)
.build();
b = (Button) findViewById(R.id.b);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
chooser.show();
}
});


chooser.setOnSelectListener(new StorageChooser.OnSelectListener() {
@Override
public void onSelect(String path) {
String par = path+"/Pictures/Screenshots";
FileObserverr obs = new FileObserverr(par);
obs.startWatching();
}
});
}
}

文件观察者类:

package com.andi.shikharshah.fileobserver;

import android.os.FileObserver;
import android.support.annotation.Nullable;
import android.util.Log;

/**
* Created by Shikhar Shah on 23-05-2017.
*/

public class FileObserverr extends FileObserver
{
String aboslutePath = "path to your directory";

public FileObserverr(String path) {
super(path,FileObserver.OPEN);
aboslutePath = path;
}

@Override
public void onEvent(int i, @Nullable String s) {
if(s!=null)
{
Log.d("TAG","File created");
}
}
}

最佳答案

引用the documentation :

Warning: If a FileObserver is garbage collected, it will stop sending events. To ensure you keep receiving events, you must keep a reference to the FileObserver instance from some other live object.

您的FileObserver可以在onClick()返回后立即进行垃圾收集。您需要在其他地方握住它,例如在 Activity 的区域中。

除此之外,您可能还需要查看 par 并确保该值符合您的预期。

关于java - onEvent 未检测到我的应用程序中的任何事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44138171/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com