5

当用户按下按钮时,我试图以编程方式锁定设备。我知道我需要使用deviceAdminReciever并且我已经这样做了,但是每当我运行它时我的应用程序就会崩溃

以下是我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.MyApp"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver
        android:name=".MainActivity"
        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-feature android:name="android.hardware.camera" />

</manifest>

以下是我的java代码:

public class MainActivity extends DeviceAdminReceiver {

public static class MyActivity extends Activity {

    protected static final int ACTIVATION_REQUEST = 1;
    private ImageButton btn;

    private DevicePolicyManager mDPM;
    private ComponentName mDeviceAdminSample;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        mDeviceAdminSample = new ComponentName(Potter.this,
                MainActivity.class);
        setContentView(R.layout.activity_main);



        btn = (ImageButton) findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                Intent intent = new Intent(
                    DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            mDeviceAdminSample = new ComponentName(this, MainActivity.class);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                    mDeviceAdminSample);
            startActivityForResult(intent, ACTIVATION_REQUEST);
            mDPM.lockNow();

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
            Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
         case ACTIVATION_REQUEST:
                if (resultCode == Activity.RESULT_OK) {
                    Log.i("DeviceAdminSample", "Administration enabled!");
                } else {
                    Log.i("DeviceAdminSample", "Administration enable FAILED!");
                }
                return;


        }
    }


}
}

但是,当我运行该应用程序时,它会崩溃。我该如何纠正?

4

2 回答 2

6

这是我的有效代码:

显现:

<application
    android:icon="@drawable/ic_power_settings_new_black_48dp"
    android:label="Screen Off"
    android:theme="@android:style/Theme.NoDisplay">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name="android.app.admin.DeviceAdminReceiver">
        <meta-data android:name="android.app.device_admin"
            android:resource="@xml/device_admin" />
        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
        </intent-filter>
    </receiver>
</application>

MainActivity.java:

package com.example.anhkhoachu.screenoff;

import android.app.Activity;
import android.app.Application;
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;


public class MainActivity extends Activity {
DevicePolicyManager dpm;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    try {
        dpm.lockNow();
        finish();
    } catch (SecurityException e) {
        Intent it = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        it.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, new ComponentName(this, DeviceAdminReceiver.class));
        startActivityForResult(it, 0);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        dpm.lockNow();
    } else {
        Toast.makeText(this, "Device administrator must be activated to lock the screen", Toast.LENGTH_LONG).show();
    }
    finish();
}}

device_admin.xml:

<device-admin> <uses-policies> <force-lock /> </uses-policies> </device-admin>

于 2016-04-18T07:12:32.360 回答
5

好的,DeviceAdminReceiver是一个广播接收器,而不是一个活动。现在,您的清单声明MainActivity了这两个组件,因此其中一个声明是不正确的。 MainActivity这个类的名字不好,因为它不是一个活动,它可能应该是MainReceiver或类似的东西(只是为了保持一致性)。

您的应用程序正在崩溃,因为 Android 正在尝试启动MainActivity,它不是一个 Activity,作为您的应用程序的主要 Activity,它无法执行此操作。

另外,根据您的代码,MyActivity是此接收器的内部类。这不是我建议坚持的范式,并且可能会导致您的一些困惑。我会将这两个实体定义为完全独立的类。如果一个必须是另一个的内部类,那么BroadcastReceiver作为Activity.

在最低限度,如果您不重构任何 Java 代码,则需要更新清单以根据您编写的内容引用正确的元素,这意味着将实际的 Activity 作为内部类引用。

<activity
    android:name=".MainActivity$MyActivity"
    android:label="@string/title_activity_main" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<receiver
    android:name=".MainActivity"
    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>

也许花点时间再次查看 SDK 中的设备管理 API 示例,该示例位于

<SDK location>/samples/<platform-version>/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.java

在你的机器上。

于 2012-12-02T19:08:12.067 回答