FIleObserver和ContentObserver在Android Marshmallow中不起作用 [英] FIleObserver and ContentObserver not working in Android Marshmallow

查看:219
本文介绍了FIleObserver和ContentObserver在Android Marshmallow中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到 FIleObserver ContentObserver 都无法在Android Marshmallow中工作的问题.我正在用这个东西来检测文件夹内发生的更改.我为棉花糖设置了运行时权限.但是在那之后它也没有显示任何事件.它可以在其他版本中完美运行.请帮我解决这个问题.

I have issue with both FIleObserver and ContentObserver not working in Android Marshmallow. I am using this thing for detecting changes that happening inside a folder. I set run time permissions for marshmallow. But after that also it shows no events. It works perfectly in other versions. Please help me to solve this problem.

首先,我尝试在Service内部使用Content Resolver来检测后台的文件夹更改.

First I tried Content Resolver inside Service for detect folder changes in background.

public class TestService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        initial();
        return START_STICKY;
    }

    public void initial(){
        getContentResolver().registerContentObserver(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                true,
                new ContentObserver(new Handler()) {
                    @Override
                    public boolean deliverSelfNotifications() {
                        Log.d("hai", "deliverSelfNotifications");
                        return super.deliverSelfNotifications();
                    }

                    @Override
                    public void onChange(boolean selfChange) {
                        super.onChange(selfChange);
                    }

                    @Override
                    public void onChange(boolean selfChange, Uri uri) {

                        if (uri.toString().matches(MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString() + "/[0-9]+")) {

                            Cursor cursor = null;
                            try {
                                cursor = getContentResolver().query(uri, new String[] {
                                        MediaStore.Images.Media.DISPLAY_NAME,
                                        MediaStore.Images.Media.DATA
                                }, null, null, null);
                                if (cursor != null && cursor.moveToFirst()) {
                                    final String fileName = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));
                                    final String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                                    // TODO: apply filter on the file name to ensure it's screen shot event
                                    Log.d("file", "FILE CHANGE OCCURED " + fileName + " " + path);
                                }
                            } finally {
                                if (cursor != null)  {
                                    cursor.close();
                                }
                            }
                        }
                        super.onChange(selfChange, uri);
                    }
                }
        );

    }
}

运行时权限为:

private void getPermission(){
    boolean hasPermission = (ContextCompat.checkSelfPermission(this,
            Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
    if (!hasPermission) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                REQUEST_READ_STORAGE);
    }
}

,并收到该权限导致出现onRequestPermissionsResult. 这种方法对我不起作用.因此,我尝试在该服务中使用FileObserver.那个时候它也可以在所有其他平台上运行,但不适用于棉花糖.

And received that permissions result in onRequestPermissionsResult. This method didn't work for me. So I tried with FileObserver inside that service. That time also it works in all other platforms, but not Marshmallow.

推荐答案

这似乎是棉花糖中的错误,

This appears to be a bug in Marshmallow, see here.

您只能通过轮询所需的任何信息来尝试解决此问题.

You can only try working around it by polling for whatever information you need.

这对您的效果如何取决于您的用例.我发现它可用于跟踪下载进度:在下载开始时以一秒钟的时间间隔开始轮询,在下载完成时停止.

How well this will work for you depends on your use case. I found it usable for tracking download progress: start polling when the download starts, with a one-second interval, and stop when the download finishes.

如果您希望更改非常少见,则可以尝试增加间隔时间-代价是更改发生和应用程序选择更改之间可能会有更大的延迟.

If you expect very infrequent changes, you can try increasing the interval – at the cost of a potentially higher delay between changes happening and your app picking them up.

这篇关于FIleObserver和ContentObserver在Android Marshmallow中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆