0

我正在尝试开发一个app用户可以通过单击按钮来锁定屏幕Android Studio。我发现了很多关于这种应用程序的问题和帖子。所以我关注了其中一个,但我收到一条错误消息并且app已关闭。

致命异常:主进程:com.example.myapplication2,PID:9360 java.lang.SecurityException:策略 #3 的 uid 10086 没有活动管理员

这是我的代码

package com.example.myapplication2;

import android.Manifest;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

String DEBUG_TAG = "testing";
Button button, b_enable, b_lock;
static final int RESULT_ENABLE = 1;
DevicePolicyManager devicePolicyManager;
ComponentName componentName;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = findViewById(R.id.button);
    b_enable = findViewById(R.id.button2);
    b_lock = findViewById(R.id.button3);

    devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    componentName = new ComponentName(MainActivity.this, Controller.class);

    boolean active = devicePolicyManager.isAdminActive(componentName);

    if (active) {
        b_enable.setText("DISABLE");
        b_lock.setVisibility(View.VISIBLE);
    } else {
        b_enable.setText("ENABLE");
        b_lock.setVisibility(View.GONE);
    }

    button.setOnClickListener(this);
    b_enable.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            boolean active = devicePolicyManager.isAdminActive(componentName);
            if (active) {
                devicePolicyManager.removeActiveAdmin(componentName);
                b_enable.setText("DISABLE");
                b_lock.setVisibility(View.VISIBLE);
            } else {
                Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                intent.putExtra (DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
                intent.putExtra (DevicePolicyManager.EXTRA_ADD_EXPLANATION, "You should enable app!");
                startActivityForResult(intent, RESULT_ENABLE);
            }
        }
    });

    b_lock.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            devicePolicyManager.lockNow();
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case RESULT_ENABLE:
            if (resultCode == Activity.RESULT_OK) {
                b_enable.setText("DISABLE");
                b_lock.setVisibility(View.VISIBLE);
            } else {
                Toast.makeText(this, "Failed!", Toast.LENGTH_SHORT).show();
            }
            return;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
};

@Override
public void onClick(View view) {
    Log.d(DEBUG_TAG, "read click");
    int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (permission != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
    }
    else{
        takeScreenshot();
    }
    }

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        if (requestCode == REQUEST_EXTERNAL_STORAGE) {
            takeScreenshot();
        }
    }
}

private void takeScreenshot() {
    Log.d(DEBUG_TAG, "start take screen shot method");
    try {
        View v1 = getWindow().getDecorView().getRootView();
        v1.setDrawingCacheEnabled(true);
        v1.buildDrawingCache();
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

        Log.d(DEBUG_TAG, "hi0");
        MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "title", null);

        Log.d(DEBUG_TAG, "stored");
    }
    catch (Throwable e) {
        e.printStackTrace();
    }
}
}

我还为强制锁定和擦除数据添加了使用策略。

4

0 回答 0