我是android新手,我想锁定手机。所以我通读了设备管理文档。我的代码是这样的。但是它似乎没有注意到它在我的清单上显示如果我在里面定义了它
我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);
}
}
}
}
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));}
}
});
}
}
您是否在"xml"文件夹下添加了一个名为"device_admin_sample.xml"的资源?它将具有为设备管理员执行"强制锁定"所需的权限。