1

我在屏幕上设置了两个按钮。每当我按下第一个按钮时,它都会禁用相机。(作为管理员)

我已经设置了清单;

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.firstapplication.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />

        </intent-filter>
    </activity>
    <!-- This is where we register our receiver -->
    <receiver
        android:name=".DemoDeviceAdminReceiver"
        android:permission="android.permission.BIND_DEVICE_ADMIN" >
        <intent-filter>

            <!-- This action is required -->
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
        </intent-filter>

        <!-- This is required this receiver to become device admin component. -->
        <meta-data
            android:name="android.app.device_admin"
            android:resource="@xml/device_admin_sample" />
    </receiver>


</application>

我将代码编写为;

public class MainActivity extends Activity  {

static final int ACTIVATION_REQUEST = 47; // identifies our request id
DevicePolicyManager devicePolicyManager;
ComponentName demoDeviceAdmin;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // ...

    // Initialize Device Policy Manager service and our receiver class
    devicePolicyManager = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
    demoDeviceAdmin = new ComponentName(this, DemoDeviceAdminReceiver.class);
}

/* begin, camera management block */
static boolean click = false ;
private OnClickListener setCamera = new OnClickListener() {
    public void onClick(View v) {
      if ( click == false ) {
          startCamera();
          click = true ;
      }
      else {
          stopCamera();
          click = false ;
      }
      activate () ; // activate device policy manager
    }
};
private void startCamera(){
        if(getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){ 
            camera.unlock();    
        } else {
                 ;
        }
    }

@SuppressLint("NewApi") private void stopCamera(){
    devicePolicyManager. setCameraDisabled(demoDeviceAdmin, true);
}

/* end, camera management block */
private OnClickListener closeApplication = new OnClickListener() {
    public void onClick(View v) {
     finish ();
    }
};
void initializeDPM ( ) {
    if (!devicePolicyManager.isAdminActive(demoDeviceAdmin)) {

            Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                    demoDeviceAdmin);
            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
            "Additional text explaining why this needs to be added.");
            startActivityForResult(intent, ACTIVATION_REQUEST);
    } else {
            devicePolicyManager.lockNow();
    }
}

public void activate ( ) {
        // Activate device administration
        Intent intent = new Intent(
        DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
        demoDeviceAdmin);
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
        "told me to do this");
        startActivityForResult(intent, ACTIVATION_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case ACTIVATION_REQUEST:
            if (resultCode == Activity.RESULT_OK) {
                // Has become the device administrator.
            } else {
                //Canceled or failed.
            }
            return;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

}

我的管理课

public class DemoDeviceAdminReceiver extends DeviceAdminReceiver{
@Override
public void onEnabled(Context context, Intent intent) {
    super.onEnabled(context, intent);

}


@Override
public void onDisabled(Context context, Intent intent) {
    super.onDisabled(context, intent);
    }
}

我的 device_admin_sample 是;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
    <limit-password />
    <watch-login />
    <reset-password />
    <force-lock />
    <wipe-data />
    <expire-password />
    <encrypted-storage />
    <disable-camera />
</uses-policies>
</device-admin>


</LinearLayout> 

为什么我不能以管理员身份关闭相机?

4

0 回答 0