0

我正在研究此参考中的电话锁。在我的三星 Tab 2 上运行良好。但是当我在 Nexus 2 上运行相同的代码时。它不起作用。我在下面发布了一些代码。它记录"DeviceAdminSample" "Admin enable FAILED!"。任何帮助将不胜感激 !!Nexus 是否有任何限制?

onCreate()
{
 deviceManger = (DevicePolicyManager)getSystemService(  
                  Context.DEVICE_POLICY_SERVICE);  
                activityManager = (ActivityManager)getSystemService(  
                  Context.ACTIVITY_SERVICE);  
                compName = new ComponentName(this, MyAdmin.class); 
}

private void EnableSetting() {
        // TODO Auto-generated method stub
      Intent intent = new Intent(DevicePolicyManager  
                 .ACTION_ADD_DEVICE_ADMIN);  
                        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,  
                                compName);  
                        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,  
                                "Additional text explaining why this needs to be added.");  
                        startActivityForResult(intent, RESULT_ENABLE);  
    }
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
         switch (requestCode) {  
             case RESULT_ENABLE:  
                 if (resultCode == Activity.RESULT_OK) {  
                     Log.i("DeviceAdminSample", "Admin enabled!");  
                 } else {  
                     Log.i("DeviceAdminSample", "Admin enable FAILED!");  
                 }  

         }  

谢谢。

4

2 回答 2

3

主要活动的代码

public class LockerTest extends Activity {
    protected static final int REQUEST_ENABLE = 0;
    DevicePolicyManager devicePolicyManager;
    ComponentName adminComponent;

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

        Button button = (Button) findViewById(R.id.btn);
        button.setOnClickListener(btnListener);

    }

    Button.OnClickListener btnListener = new Button.OnClickListener() {
        public void onClick(View v) {
            adminComponent = new ComponentName(LockerTest.this, Darclass.class);
            devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);

            if (!devicePolicyManager.isAdminActive(adminComponent)) {

                Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminComponent);
                startActivityForResult(intent, REQUEST_ENABLE);
            } else {
                devicePolicyManager.lockNow();
            }

        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (REQUEST_ENABLE == requestCode) {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}

创建一个新类——Darclass——代码

import android.app.admin.DeviceAdminReceiver;

public class Darclass extends DeviceAdminReceiver{

}

在“res”中创建一个文件夹“xml”。然后在“xml”文件夹中创建 my_admin.xml 文件。my_admin.xml 的代码。注意在前后添加此接收器

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

最后将下面给出的接收器添加到您的 AndroidManifest.xml

<receiver
            android:name=".Darclass"
            android:permission="android.permission.BIND_DEVICE_ADMIN" >
            <meta-data
                android:name="android.app.device_admin"
                android:resource="@xml/my_admin" />

            <intent-filter>
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
            </intent-filter>
        </receiver>

它应该可以在您的设备上运行。

于 2013-10-02T06:27:10.673 回答
1

我有同样的问题。这是因为我的 ... 块在我的 AndroidManifest.xml 块之外。当我移动 ... 块时效果很好,如下所示:

        <activity
            android:name=".Main"
            android:label="@string/app_name"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name="MyDeviceAdminReceiver"
            android:permission="android.permission.BIND_DEVICE_ADMIN">
            <meta-data android:name="android.app.device_admin" android:resource="@xml/device_admin_xml" />
            <intent-filter>
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
                <action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLE_REQUESTED" />
                <action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLED" />
            </intent-filter>
        </receiver>
于 2014-09-18T18:39:15.423 回答