Android Device Admin not working

☆樱花仙子☆ 提交于 2020-01-03 03:24:05

问题


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

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>

My Code

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

My Other Activity

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


}

回答1:


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.



来源:https://stackoverflow.com/questions/7848155/android-device-admin-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!