我想观察是否有文件夹被添加到了 /storage/emulated/0/DCIM/Camera
在服务中使用FileObserver,但是没有成功。如果我的代码有什么问题,请告诉我🙏。下面是源码。
AndroidManifest.xml
我加了 使用权限 和 服务 标签。
<?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
我补充道 文件观察者 内的服务,并推翻了其 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如期工作了