i have issue both fileobserver , contentobserver not working in android marshmallow. using thing detecting changes happening inside folder. set run time permissions marshmallow. after shows no events. works in other versions. please me solve problem.
first tried content resolver inside service 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 file name ensure it's screen shot event log.d("file", "file change occured " + filename + " " + path); } } { if (cursor != null) { cursor.close(); } } } super.onchange(selfchange, uri); } } ); } }
and run time permissions as:
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); } }
and received permissions result in onrequestpermissionsresult
. method didn't work me. tried fileobserver
inside service. time works in other platforms, not marshmallow.
this appears bug in marshmallow, see here.
you can try working around polling whatever information need.
how work depends on use case. found usable tracking download progress: start polling when download starts, one-second interval, , stop when download finishes.
if expect infrequent changes, can try increasing interval – @ cost of potentially higher delay between changes happening , app picking them up.
Comments
Post a Comment