How to close ACTION_USAGE_ACCESS_SETTINGS intent when user click correct target application?

前端 未结 1 1595
梦谈多话
梦谈多话 2021-02-14 05:27

I am using Intent ACTION_USAGE_ACCESS_SETTINGS in setting (Settings->Security->Apps with usage access) to use UsageStatsManager in t

1条回答
  •  温柔的废话
    2021-02-14 05:49

    AppOpsManager has another interesting method:

    void startWatchingMode (String op, String packageName, AppOpsManager.OnOpChangedListener callback)

    from documentation:

    Monitor for changes to the operating mode for the given op in the given app package.

    Basically this is what you expect -> react on permission to read package usage stats switch change.

    What you need to do is check the allowance state and if it is not granted, open the Settings and create a listener:

    private boolean hasPermissionToReadNetworkHistory() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return true;
        }
        final AppOpsManager appOps = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
        int mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
                android.os.Process.myUid(), getPackageName());
        if (mode == AppOpsManager.MODE_ALLOWED) {
            return true;
        }
        appOps.startWatchingMode(AppOpsManager.OPSTR_GET_USAGE_STATS,
                getApplicationContext().getPackageName(),
                new AppOpsManager.OnOpChangedListener() {
                    @Override
                    @TargetApi(Build.VERSION_CODES.KITKAT)
                    public void onOpChanged(String op, String packageName) {
                        int mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
                                android.os.Process.myUid(), getPackageName());
                        if (mode != AppOpsManager.MODE_ALLOWED) {
                            return;
                        }
                        appOps.stopWatchingMode(this);
                        Intent intent = new Intent(MainActivity.this, MainActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                        getApplicationContext().startActivity(intent);
                    }
                });
        requestReadNetworkHistoryAccess();
        return false;
    }
    
    private void requestReadNetworkHistoryAccess() {
        Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
        startActivity(intent);
    }
    

    Be careful and remove the listener calling the method:

    void stopWatchingMode (AppOpsManager.OnOpChangedListener callback)

    The example above is just opening main application activity. However you might add any behaviour:

    • pass extra intent parameter to inform just opened activity to show dialog to user
    • Start background service to show toast, that user must grant the permission.
    • etc...

    Check out this repository, as it demonstrates this case and the overall usage of NetworkStats.

    0 讨论(0)
提交回复
热议问题