3

我正在关注设备管理应用程序的示例代码。我尝试启动使用户能够添加设备管理应用程序的活动失败。以下是我用来尝试启动它的代码。

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>

我什至没有收到错误。我的活动开始了,但没有任何事情发生,甚至没有错误。

4

2 回答 2

0

也许布局有问题。如果您没有收到错误,那么它没有崩溃,如果它没有崩溃,那么唯一可能出错的是显示(布局)。

于 2012-06-30T08:21:28.173 回答
-2
 final Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.DeviceAdminAdd"));
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminReceiver);
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "To use all features activate it.");
        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
        startActivityForResult(intent, ACTIVATION_REQUEST);
于 2014-10-24T05:18:35.990 回答