Android设备管理员无法正常工作 [英] Android Device Admin not working

查看:73
本文介绍了Android设备管理员无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android的新手,我想锁定手机.因此,我阅读了Device Admin文档.并让我的代码看起来像这样.但这似乎并没有在我的清单上注意到它暗示我是否已在其中定义它

I am new in android and i would like to lock the phone. So I Read through Device Admin documentation. and have my code looking like this. But it doesnt seem to notice it on my manifest suggests if i have it defined in it which i do

我的manifest.xml

my manifest.xml

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainLauncher"android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Controller"android:label="@string/app_name">
</activity> 

<activity android:name=".DeviceAdminSample$Controller"android:label="@string/activity_sample_device_admin">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>

</activity>

<receiver android:name=".DeviceAdminSample"android:label="@string/sample_device_admin"android:description="@string/sample_device_admin_description"android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"android:resource="@xml/device_admin_sample" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>

</application>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
</manifest>

我的代码

public class DeviceAdminSample extends DeviceAdminReceiver {

public static class Controller extends Activity {
            static final int RESULT_ENABLE = 1;

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

        mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
        mAM = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
        mDeviceAdminSample = new ComponentName(Controller.this, DeviceAdminSample.class);

        mEnableButton = (Button)this.findViewById(R.id.button1);
        mEnableButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                        mDeviceAdminSample);
                intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                        "Additional text explaining why this needs to be added.");
                startActivityForResult(intent, RESULT_ENABLE);
            }
        });

        mDisableButton = (Button)this.findViewById(R.id.button2);
        mDisableButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                mDPM.removeActiveAdmin(mDeviceAdminSample);
                updateButtonStates();
            }
        });

        mForceLockButton = (Button)this.findViewById(R.id.button3);
        mForceLockButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Log.i("                                                                                                            In","In the forc lock button");
                if (mAM.isUserAMonkey()) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(Controller.this);
                    builder.setMessage("You can't lock my screen because you are a monkey!");
                    builder.setPositiveButton("I admit defeat", null);
                    builder.show();
                    return;
                }
                boolean active = mDPM.isAdminActive(mDeviceAdminSample);
                if (active) {
                v   mDPM.lockNow();
                }
            }
        });
    }
    void updateButtonStates() {
        boolean active = mDPM.isAdminActive(mDeviceAdminSample);
        if (active) {
            mForceLockButton.setEnabled(true);
        } else {
            mForceLockButton.setEnabled(false);
        }
    }
}
}

我的其他活动

package com.examples;


public class MainLauncher extends Activity {


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

Button btnLock = (Button)this.findViewById(R.id.button22);
btnLock.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);  
        KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);

        lock.reenableKeyguard();

    }
});

Button btnDevice = (Button)this.findViewById(R.id.button11);
btnDevice.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
    try{                    
        Intent openScreen = new Intent();
        openScreen.setClass(MainLauncher.this, DeviceAdminSample.class);
        startActivity(openScreen);
    }
        catch(Exception e)
        {Log.i("DeviceAdmin button",String.valueOf(e));}
    }
});
}


}

推荐答案

您是否已在名为"device_admin_sample.xml"的"xml"文件夹下添加了资源?它将具有执行强制锁定"所需的权限,以使设备管理员能够正常工作.

Have you added a resource under "xml" folder called "device_admin_sample.xml"? It will have permission required for doing a "force lock" for Device administrator to work.

这篇关于Android设备管理员无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆