Question

I am working on Phone Lock from this Reference. Working well on my Samsung Tab 2. But when I run the Same Code On Nexus 2. It does not work .I have posted some Code below. It Logs "DeviceAdminSample" "Admin enable FAILED!". Any help will be Appreciated !! Is there any Restriction on 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!");  
                 }  

         }  

Thanks.

Was it helpful?

Solution

Code for main Activity

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);
        }
    }
}

Create a new class - Darclass - code

import android.app.admin.DeviceAdminReceiver;

public class Darclass extends DeviceAdminReceiver{

}

Create a folder 'xml' in 'res'. Then create my_admin.xml file in 'xml' folder. Code for my_admin.xml. Note add this receiver after and before

<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>

Finally add the receiver given bellow to your 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>

It should work on your device.

OTHER TIPS

I had this same problem. It was because my ... block was outside of my block in AndroidManifest.xml. Worked perfectly when I moved the ... block, as shown here:

        <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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top