Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 以编程方式设置“始终在VPN上”;管理员不拥有该配置文件“;_Android_Device Policy Manager_Android Vpn Service - Fatal编程技术网

Android 以编程方式设置“始终在VPN上”;管理员不拥有该配置文件“;

Android 以编程方式设置“始终在VPN上”;管理员不拥有该配置文件“;,android,device-policy-manager,android-vpn-service,Android,Device Policy Manager,Android Vpn Service,我正试图弄清楚如何配置我的VPN应用程序,以便在应用程序中使用切换开关切换“始终打开”标志 我知道 然而,如何使用这个函数还不是很清楚。我尝试了以下方法: Admin.java public class AdvancedSettings extends AppCompatActivity implements View.OnClickListener { private ComponentName componentName; private DeviceP

我正试图弄清楚如何配置我的VPN应用程序,以便在应用程序中使用切换开关切换“始终打开”标志

我知道

然而,如何使用这个函数还不是很清楚。我尝试了以下方法:

Admin.java

public class AdvancedSettings extends AppCompatActivity 
        implements View.OnClickListener {

    private ComponentName componentName;
    private DevicePolicyManager devicePolicyManager;
    private boolean alwaysOnConfiguredValue;

    private static final int ALWAYS_ON_REQUEST_CODE = 11;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_advanced);

        Button button = findViewById(R.id.toggleAlwaysOnButton);
        button.setOnClickListener(this);

        devicePolicyManager = (DevicePolicyManager) this
                .getSystemService(Context.DEVICE_POLICY_SERVICE);
        componentName = new ComponentName(
                this.getApplicationContext(), Admin.class);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.toggleAlwaysOnButton) {
            this.setAlwaysOn(true);
        }
    }

    /**
     * Handle the Activity Result.
     */
    @Override
    protected void onActivityResult(
        int requestCode, int resultCode, @Nullable Intent data
    ) {
        if (requestCode == ALWAYS_ON_REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK) {
                finalizeAlwaysOnToggle();
            } else {
                Log.w(
                    "Invalid result code " + resultCode
                );
            }
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * Start the process of enabling "Always On" for the VPN.
     *
     * @param boolean value
     */
    private void setAlwaysOn(boolean value) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            alwaysOnConfiguredValue = value;

            if (devicePolicyManager.isAdminActive(componentName)) {
                finalizeAlwaysOnToggle();
                return;
            }

            requestAdminAccess();
        } else {
            Toas.makeText(this, "Not supported", Toast.LENGTH_LONG).show();
        }
    }

    /**
     * Request Admin Access for this application 
     * if it has not already been done.
     */
    private void requestAdminAccess() {
        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
        intent.putExtra(
            DevicePolicyManager.EXTRA_ADD_EXPLANATION,
            "This is required to modify the Always-On Feature from within the Test Application."
        );
        this.startActivityForResult(intent, ALWAYS_ON_REQUEST_CODE);
    }

    /**
     * Finalize setting the always on toggle after the Admin Access 
     * has been granted for this application.
     */
    private void finalizeAlwaysOnToggle() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            try {
                if (devicePolicyManager.isAdminActive(componentName)) {
                    devicePolicyManager.setAlwaysOnVpnPackage(
                        componentName, (alwaysOnConfiguredValue) ? "com.myapp" : null, true
                    );
                } else {
                    Log.e(
                        "Device Policy Manager Admin is not yet active while " + 
                        "trying to finalize changes to AlwaysOnToggle."
                    );
                }
            } catch (PackageManager.NameNotFoundException e) {
                Log.e("Unable to set always on vpn due to NameNotFound Exception.", e);
            }
        }
    }
}
公共类管理员扩展DeviceAdminReceiver{
@凌驾
public void onEnabled(@NonNull上下文,@NonNull意图){
super.onEnabled(上下文、意图);
}
}
AdvancedSettings.java

public class AdvancedSettings extends AppCompatActivity 
        implements View.OnClickListener {

    private ComponentName componentName;
    private DevicePolicyManager devicePolicyManager;
    private boolean alwaysOnConfiguredValue;

    private static final int ALWAYS_ON_REQUEST_CODE = 11;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_advanced);

        Button button = findViewById(R.id.toggleAlwaysOnButton);
        button.setOnClickListener(this);

        devicePolicyManager = (DevicePolicyManager) this
                .getSystemService(Context.DEVICE_POLICY_SERVICE);
        componentName = new ComponentName(
                this.getApplicationContext(), Admin.class);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.toggleAlwaysOnButton) {
            this.setAlwaysOn(true);
        }
    }

    /**
     * Handle the Activity Result.
     */
    @Override
    protected void onActivityResult(
        int requestCode, int resultCode, @Nullable Intent data
    ) {
        if (requestCode == ALWAYS_ON_REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK) {
                finalizeAlwaysOnToggle();
            } else {
                Log.w(
                    "Invalid result code " + resultCode
                );
            }
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * Start the process of enabling "Always On" for the VPN.
     *
     * @param boolean value
     */
    private void setAlwaysOn(boolean value) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            alwaysOnConfiguredValue = value;

            if (devicePolicyManager.isAdminActive(componentName)) {
                finalizeAlwaysOnToggle();
                return;
            }

            requestAdminAccess();
        } else {
            Toas.makeText(this, "Not supported", Toast.LENGTH_LONG).show();
        }
    }

    /**
     * Request Admin Access for this application 
     * if it has not already been done.
     */
    private void requestAdminAccess() {
        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
        intent.putExtra(
            DevicePolicyManager.EXTRA_ADD_EXPLANATION,
            "This is required to modify the Always-On Feature from within the Test Application."
        );
        this.startActivityForResult(intent, ALWAYS_ON_REQUEST_CODE);
    }

    /**
     * Finalize setting the always on toggle after the Admin Access 
     * has been granted for this application.
     */
    private void finalizeAlwaysOnToggle() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            try {
                if (devicePolicyManager.isAdminActive(componentName)) {
                    devicePolicyManager.setAlwaysOnVpnPackage(
                        componentName, (alwaysOnConfiguredValue) ? "com.myapp" : null, true
                    );
                } else {
                    Log.e(
                        "Device Policy Manager Admin is not yet active while " + 
                        "trying to finalize changes to AlwaysOnToggle."
                    );
                }
            } catch (PackageManager.NameNotFoundException e) {
                Log.e("Unable to set always on vpn due to NameNotFound Exception.", e);
            }
        }
    }
}
它可以很好地处理添加设备管理员的请求,但是在完成之后,当它在调用
devicePolicyManager.setAlwaysOnVpnPackage
期间运行
finalizeAlwaysOnToggle()
时,我收到以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.myapp, PID: 30778
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=11, result=-1, data=null} to activity {com.myapp/com.myapp.ui.settings.AdvancedSettings}: java.lang.SecurityException: Admin ComponentInfo{com.myapp/com.myapp.provider.Admin} does not own the profile

您必须区分“设备管理员”、“设备所有者”和“配置文件所有者”。 正如文档中所述,您需要是后两个中的一个,才能调用
setAlwaysOnVpnPackage

由设备或配置文件所有者调用以配置始终在线的VPN 通过当前用户的特定应用程序进行连接。这 重新启动后,将自动授予并保持连接


()

那么,有没有一种方法可以通过编程方式请求这两种方法中的任何一种?这是一个主题,您可能想从这里开始,例如:您解决了这个问题吗?我也犯了同样的错误。