Я хочу посмотреть каталог камеры. Но мой код не работает. Сервис запущен, но я не могу ловить события. Я запускаю приложение, затем запускаю камеру. Я добавляю свой сервис в манифест с android: enabled = "true", android: exported = "false".
public class MyService extends Service {
public MyService() {
}
public static FileObserver observer;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final String pathToWatch = android.os.Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera/";
Log.i("Main123", "My Service Started and trying to watch " + pathToWatch);
observer = new FileObserver(pathToWatch) {
@Override
public void onEvent(int event, String file) {
Log.i("Main123", "File created [" + pathToWatch + file + "]");
Toast.makeText(getBaseContext(), file + " was saved!", Toast.LENGTH_LONG).show();
}
};
observer.startWatching();
return Service.START_NOT_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
//TODO for communication return IBinder implementation
return null;
}}
publi c Класс MainActivity расширяет AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
},
1);
} else {
Intent intent = new Intent(this, MyService.class);
this.startService(intent);
}
}}