我正在关注设备管理应用程序的示例代码。我尝试启动使用户能够添加设备管理应用程序的活动失败。以下是我用来尝试启动它的代码。
public class DeviceAdminTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ComponentName deviceAdminReceiver = new ComponentName(this, TestAdminReceiver.class);
Intent i = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
i.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceAdminReceiver);
i.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "My Explanantion");
// startActivityForResult(i, 1);
try{
startActivity(i);
}catch(Exception ex){
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG);
}
}
}
以下是接收器的代码...
public class TestAdminReceiver extends DeviceAdminReceiver {
@Override
public void onEnabled(Context context, Intent intent) {
Toast.makeText(context, "On Enabled", Toast.LENGTH_SHORT).show();
}
@Override
public CharSequence onDisableRequested(Context context, Intent intent) {
return ("You are about to disable device admin");
}
@Override
public void onDisabled(Context context, Intent intent) {
Toast.makeText(context, "On Disabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onPasswordChanged(Context context, Intent intent) {
Toast.makeText(context, "On Password Changed", Toast.LENGTH_SHORT).show();
}
}
最后,这是清单……
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.zeezulander.admintest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="org.zeezulander.admintest.DeviceAdminTestActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".TestAdminReceiver" android:permission="android.permission.BIND_DEVICE_ADMIN" android:label="Test Admin">
<meta-data android:resource="@xml/admin_data" android:name="adminMetaData"/>
<intent-filter >
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
</intent-filter>
</receiver>
</application>
我什至没有收到错误。我的活动开始了,但没有任何事情发生,甚至没有错误。