android - How to allow your app with usage access? -


from android lollipop, app wants access usage of other apps needs enabled security. rather manually enabling manually settings -> security -> apps usage access, want enable programmatically. cannot find clear answer anywhere. can me on one?

try in activity:

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     if (!isaccessgranted()) {         intent intent = new intent(settings.action_usage_access_settings);         startactivity(intent);     } }   private boolean isaccessgranted() {     try {         packagemanager packagemanager = getpackagemanager();         applicationinfo applicationinfo = packagemanager.getapplicationinfo(getpackagename(), 0);         appopsmanager appopsmanager = (appopsmanager) getsystemservice(context.app_ops_service);         int mode = appopsmanager.checkopnothrow(appopsmanager.opstr_get_usage_stats,                 applicationinfo.uid, applicationinfo.packagename);         return (mode == appopsmanager.mode_allowed);      } catch (packagemanager.namenotfoundexception e) {         return false;     } } 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -