本文介绍了锁定Android手机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在用户按下按钮时以编程方式锁定设备.我知道我将需要使用 deviceAdminReciever ,并且这样做了,但是我的应用程序每次运行都会崩溃

I am trying to lock a device programmatically when the user presses a button. I am aware that I will need to use deviceAdminReciever and I have done so but my app crashes whenever I run it

以下是我的清单:

<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代码:

The following is my java code:

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;


        }
    }


}
}

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

However, when I run the app it crashes. How can I correct this?

推荐答案

好, DeviceAdminReceiver 是BroadcastReceiver,而不是Activity.现在,您的清单对这两个组件都声明了 MainActivity ,因此这些声明之一是不正确的. MainActivity 对该类来说是个坏名字,因为它不是Activity,它可能应该是 MainReceiver 或类似的东西(出于一致性的考虑).

OK, DeviceAdminReceiver is a BroadcastReceiver, not an Activity. Right now, your manifest declares MainActivity for both components, so one of those declarations is incorrect. MainActivity is a bad name for this class since it is not an Activity, it should probably be MainReceiver or something like that (just for consistency's sake).

您的应用程序崩溃是因为Android试图启动不是Activity的 MainActivity 作为应用程序的主要Activity,而它无法执行.

Your application is crashing because Android is trying to start MainActivity, which is not an Activity, as the main Activity of your application, which it cannot do.

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

Also, according to your code, MyActivity is an inner class of this receiver. This is not a paradigm I would recommend sticking with and may be leading to some of your confusion. I would define both of these entities as completely separate classes. If one MUST be an inner class of the other, the BroadcastReceiver will make more sense as an inner class of the Activity.

在BARE MINIMUM,如果您不重构任何Java代码,则需要更新清单以根据所写内容引用适当的元素,这意味着将实际的Activity引用为内部类.

At the BARE MINIMUM, if you don't refactor any of your Java code, you need to update your manifest to reference the proper elements based on what you've written, which means referencing the actual Activity as an inner class.

<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示例,该示例位于

Perhaps take a moment to review again the Device Administration API Sample in the SDK, which is located at

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

在您的计算机上.

这篇关于锁定Android手机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:41