- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在服务中使用 FileObserver 观察是否在 /storage/emulated/0/DCIM/Camera
中添加了任何文件/文件夹,但它不起作用。如果我的代码有什么问题请告诉我🙏。这是源代码:
AndroidManifest.xml
我添加了 uses-permission 和 service 标签:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dtautosender">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service android:name=".LogWatcherService"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
LogWatcherService.java
我在服务中添加了FileObserver,并重写了它的onEvent方法:
package com.example.dtautosender;
import android.app.Service;
import android.content.Intent;
import android.os.FileObserver;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.Nullable;
import java.io.File;
public class LogWatcherService extends Service {
private static final String TAG = "LogWatcherService";
public static FileObserver fo;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final File directoryToWatch = new File(android.os.Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera/");
fo = new FileObserver(directoryToWatch.getAbsolutePath(), FileObserver.ALL_EVENTS) {
@Override
public void onEvent(int event, @Nullable String path) {
Log.d(TAG, "onEvent: " + path);
}
};
fo.startWatching();
Toast.makeText(this, "Log Watcher Service is started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStartCommand: Log Watcher Service is started and trying to watch: " + directoryToWatch);
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Log Watcher Service is stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy: Log Watcher Service is stopped");
fo.stopWatching();
}
}
MainActivity.java
在授予存储权限后打开应用程序时,我会启动该服务:
package com.example.dtautosender;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1;
private static final String TAG = "MainActivity";
private boolean watcherIsEnabled = false;
private Context ctx;
private Button toggleWatcherButton;
private TextView statusTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ctx = this;
statusTextView = findViewById(R.id.status_text);
toggleWatcherButton = findViewById(R.id.watcher_toggle);
addEventListeners();
requestPermissions();
}
private void addEventListeners() {
toggleWatcherButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startLogWatcherService();
}
});
}
private void requestPermissions() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
Log.d(TAG, "requestPermissions: Permission isn't granted");
} else {
ActivityCompat.requestPermissions(
this,
new String[] {
Manifest.permission.READ_EXTERNAL_STORAGE
},
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
}
} else {
Log.d(TAG, "startLogWatcherService: Permission has already been granted");
startLogWatcherService();
}
}
private void startLogWatcherService() {
if (!watcherIsEnabled) {
startService(new Intent(ctx, LogWatcherService.class));
statusTextView.setText("Status: Active");
statusTextView.setTextColor(Color.parseColor("#00FF00"));
toggleWatcherButton.setText("Stop Service");
} else {
stopService(new Intent(ctx, LogWatcherService.class));
statusTextView.setText("Status: Non-Active");
statusTextView.setTextColor(Color.parseColor("#FF0000"));
toggleWatcherButton.setText("Start Service");
}
watcherIsEnabled = !watcherIsEnabled;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startLogWatcherService();
} else {
toggleWatcherButton.setText("Watch file permission isn't granted by the user");
toggleWatcherButton.setEnabled(false);
}
return;
}
}
}
}
我启动了应用程序,日志上有 onStartCommand。但当发生变化时,永远不会调用 onEvent 方法。
最佳答案
这个问题我自己解决了。我认为问题是因为我的 Android Studio API 级别是 30,其中“Environment.getExternalStorageDirectory()”已被弃用。因此,为了解决这个问题,我将以下属性添加到 AndroidManifest.xml 中的应用程序标记
android:requestLegacyExternalStorage="true"
现在,我的 FileObserver 正在按预期工作
关于java - 无法使用 FileObserver 观察文件夹内的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61742222/
我正在使用 FileObserver 来查看 android 中的文件夹。当用户在这个文件夹中修改或创建一个新文件时,FileObserver 应该对这个文件做一些事情。 问题是通过使用子句 File
我有一个目录,用户可以将文件从另一台设备传输到该目录。我有一个 FileObserver 监视那个目录。但我不知道要注意什么。我需要知道文件何时完成传输,以便将其移动到另一个位置。那么我要注意什么事件
我遇到了以下问题: 在我的 Activity 中,我有一个列出外部文件目录内容的 ListView 。我希望列表在内容发生任何变化时立即更新,所以我有: public class FileAdapte
我在 Activity 的 onCreate 函数中声明了 1 个 fileObserver。这是第一次,每当您对文件目录进行任何更改(具体而言,我删除了文件夹)时,观察者都会工作并广播一个 Inte
我正在一个家庭更换应用程序的主要 Activity 中实现一个文件观察器。它会耗尽电池电量吗? 谢谢 最佳答案 FileObserver 不应耗尽电池电量。 FileObserver 监听 iNoti
我需要监控文件夹中的所有文件,当文件打开时(FileObserver.OPEN)我想执行一个方法。问题是有时,FileObserver 实例被 GC 收集,我试过这个: final MyFil
这是我的代码: public class SyncNotifyService extends Service { private final static String TAG = "File
好的,这很简单:我有一个 FileObserver 类来观察带有音乐的文件夹。所以我实现了 onEvent 和所有这些东西,但是当我使用文件管理器将文件移动或粘贴到该文件夹时,我得到的不是 Fil
根据文档, "Each FileObserver instance monitors a single file or directory. If a directory is monitored,
我怀疑我已经知道这个问题的答案了,但我还是想请教专家。 是否可以使用 FileObserver 类来监视对属于其他应用程序的文件的更改(即/data 目录中不属于实现 FileObserver 的应用
在 Android FileObserver 中,当在观察文件夹中创建目录或在该文件夹中删除目录时,我会收到一些奇怪的事件编号,例如 (1073742080, 1073742336)。 对于文件夹的创
在我的 Android 应用程序中,我想检测目录中的事件。这是代码: String path = Environment.getExternalStorageDirectory()
我很想知道是否有比 FileObserver 更好的方法来监视目录的文件更改/创建/删除。 FileObserver 需要持续引用它,这需要持续运行的服务,这很困难,也是一种不好的做法,对吧? 对于如
我想在服务中使用 FileObserver 观察是否在 /storage/emulated/0/DCIM/Camera 中添加了任何文件/文件夹,但它不起作用。如果我的代码有什么问题请告诉我🙏。这是
我需要在一个目录上设置一个 FileObserver。观察期链接到一个长时间运行的 Activity ,长时间运行是因为它有一个包含很多页面的 ViewPager。我没有将 FileObserver
我正在使用 FileObserver观察目录的变化。该过程在 90% 的时间里运行良好,但偶尔会失败。 这是一个 logcat 的工作示例: 04-23 21:12:03.873: V/ItemObs
在我的应用程序中,我有不同的组件,它们使用 FileObservers 监视 sdcard 中的特定文件。所以有两个 File Observer 实例观察一个文件,比如 abc.xml 的所有事件。
我想监控android系统文件的变化 "/sys/class/net/eth0/operstate" 用于监控以太网状态。 我写了一个 File Observer 类,但我没有收到任何事件public
我正在编写一个使用 android 中的 fileobserver 的应用程序。当我以任何方式修改文件(创建等)时,它应该上传。为了实现一个队列并防止使用线程等耗尽内存,我想使用 IntentServ
我现在正在编写 FileObserver 代码。我有以下代码,如果运行它,我将收到 NullPointerException。 public class FileModificationService
我是一名优秀的程序员,十分优秀!