Android设备管理员无法正常工作

时间:2011-10-21 10:30:01

标签: android device-admin

我是Android新手,我想锁定手机。所以我通读了Device Admin文档。让我的代码看起来像这样。但它似乎没有注意到我的清单上的它表明如果我在其中定义它我做

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


}

1 个答案:

答案 0 :(得分:1)

您是否在名为“device_admin_sample.xml”的“xml”文件夹下添加了资源?它将具有为设备管理员执行“强制锁定”所需的权限。