Android Lollipop know if app as Usage Stats access -


since android lollipop, have api accessing apps usage stats. however, app must granted permissions user.

i know redirecting user settings using settings.action_usage_access_settings.

now, question how know user has granted permissions can stop redirecting him settings.

thanks!

you can query usagestats daily interval , end time current time , if nothing returned means user hasn’t granted permissions

@targetapi(build.version_codes.lollipop) public boolean doihavepermission(){       final usagestatsmanager usagestatsmanager = (usagestatsmanager) context.getsystemservice(context.usage_stats_service);     final list<usagestats> queryusagestats = usagestatsmanager.queryusagestats(usagestatsmanager.interval_daily, 0,  system.currenttimemillis());      return !queryusagestats.isempty(); } 

daily interval start date 0 , end date current time must @ least return todays usage.so empty if permissions not granted.


Comments

Popular posts from this blog

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -