How many days does Android use action? Usage? Access? Settings permission check that the mobile phone does not use other apps

1 demand

Find out how many days the phone hasn't used all the apps

 

 

 

 

 

2 ideas

Get the package name of the APP that has been used for several days through the action "usage" access "permission, and then get all the user apps of the mobile phone. The remaining APP is the APP that has not been used for many days.

 

 

 

 

 

3 action? Usage? Access? Settings permission application

1) Define permissions in the Android manifest.xml file

    <uses-permission
        android:name="android.permission.PACKAGE_USAGE_STATS"
        tools:ignore="ProtectedPermissions" />

 

2) After the permission is declared, you need to apply through the Intent startup method, open the corresponding interface, and agree to the current APP permission.

            val intent = Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS)
            startActivityForResult(intent, REQUEST_SYSTEM_USAGE_ACCESS_CODE)

 

 

 

 

 

 

 

 

4. Judge whether to obtain action? Usage? Access? Settings permission

    /**
     * Determine whether the application permission with permission to view usage has been obtained
     *
     * @param context
     * @return
     */
    public static boolean isStatAccessPermissionSet(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            try {
                PackageManager packageManager = context.getPackageManager();
                ApplicationInfo info = packageManager.getApplicationInfo(context.getPackageName(), 0);
                AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
//                appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, info.uid, info.packageName);
                return appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, info.uid, info.packageName) == AppOpsManager.MODE_ALLOWED;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        } else {
            return true;
        }
    }

 

    /**
     * Viewing is an application interface for viewing usage
     *
     * @return
     */
    public static boolean isNoOption(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            PackageManager packageManager = context.getPackageManager();
            Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
            List<ResolveInfo> list = new ArrayList<>();
            //Inter process communication may be abnormal
            try {
                list.addAll(packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY));
            } catch (Exception e) {
                e.printStackTrace();
            }
            return list.size() > 0;
        }
        return false;
    }
    /**
     * Authority check
     */
    fun checkUsedPermission():Boolean {
        if(!isStatAccessPermissionSet(this) && isNoOption(this)) {
            return false
        } else {
            return true
        }
    }

Because we used startActivityForResult to open the interface at the beginning, we can judge and re process the permission in onActivityResult

 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        Log.i(TAG, "onActivityResult start requestCode is:" + requestCode)
        if (requestCode == REQUEST_SYSTEM_USAGE_ACCESS_CODE) {
            var result = checkUsedPermission()
            Log.i(TAG, "onActivityResult result is ${result}")
            if (result) {
                init()
            } else {
                //finish
                Log.i(TAG, "has no ACTION_MANAGE_WRITE_SETTINGS");
                ToastUtils.showShort(R.string.text_please_allow_permisson)
                finish()
            }
        }
    }

 

 

 

 

 

5. How long does it take to detect the use of other apps

 /**
         * How many hours does the query use app
         */
        fun getRuningAppsNoContainSelf(context: Context, hour: Int): List<String> {
            val runingApps = ArrayList<String>()
            val sUsageStatsManager = context.getSystemService(Context.USAGE_STATS_SERVICE) as UsageStatsManager
            val endTime = System.currentTimeMillis()
            val beginTime = endTime - TimeUnit.HOURS.toMillis(hour.toLong())
            val event = UsageEvents.Event()
            val usageEvents = sUsageStatsManager.queryEvents(beginTime, endTime)
            while (usageEvents.hasNextEvent()) {
                usageEvents.getNextEvent(event)
                if (event.eventType == UsageEvents.Event.MOVE_TO_FOREGROUND) {
                    val pkg = event.packageName
                    LogUtil.i("Get used apps", "pkg by ${pkg}")
                    if (!TextUtils.isEmpty(pkg) && !CLEAN_NOT_CONTAIN_LIST.contains(pkg)) {
                        if (!runingApps.contains(pkg)) {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            }
                            runingApps.add(event.packageName)
                        }
                    }
                }
            }
            return runingApps
        }

For example, in 10 days, our hour parameter passes 10 * 24

Tags: Android Mobile xml

Posted on Sun, 10 May 2020 10:57:11 -0400 by pklover