5

使用 Android 以编程方式锁定屏幕。我从 Stackoverflow 以前的问题中得到了一些关于此的好主意,并且我做得很好,但是当我运行该代码时,没有异常和错误。而且,屏幕没有锁定。请在这段代码中指出我的错误我的 manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.top"
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=".LockActivity">
        </activity>
    <activity
        android:name=".LockActivity$Myclass"
        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=".LockActivity"
    android:label="device_admin"
    android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
           android:resource="@xml/my_admin" />
<intent-filter>
    <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
</manifest>

我的班级文件

package net.top;


 import android.app.Activity;
import android.app.KeyguardManager;

import android.app.KeyguardManager.KeyguardLock;
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.os.PowerManager;
import android.widget.Toast;

public class LockActivity extends DeviceAdminReceiver{

public void onEnabled(Context context, Intent intent) {
    showToast(context, "Sample Device Admin: enabled");
    }

    @Override
    public CharSequence onDisableRequested(Context context, Intent intent) {
    return "This is an optional message to warn the user about disabling.";
    }

    @Override
    public void onDisabled(Context context, Intent intent) {
    showToast(context, "Sample Device Admin: disabled");
    }
    void showToast(Context context, CharSequence msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();

    }

public static class Myclass extends Activity {

protected PowerManager.WakeLock mWakeLock;
DevicePolicyManager mDPM;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  //  setContentView(R.layout.main);

    mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
    //  mDPM.lockNow();

    ComponentName mAdminName = new ComponentName(Myclass.this,LockActivity.class);

        if(!mDPM.isAdminActive(mAdminName))
        {
            Intent intent = new       
Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,    
mAdminName);
            //mDPM.lockNow();
        }
        else
        {
            mDPM.lockNow();
        }   



         }
}

}

我还添加了 My_admin.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
    <limit-password />
    <watch-login />
    <reset-password />
    <force-lock />
    <wipe-data />
    <expire-password />
    <encrypted-storage />
    <disable-camera />
</uses-policies>
</device-admin>

我找不到这里出了什么问题。请帮助我在这里指出我的错误。提前致谢。

4

1 回答 1

2

您可以将此代码添加到您的 Activity 类中,它对我来说很好用

    if(!mDPM.isAdminActive(mAdminName))
        {
             intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
              intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"is locked");
              intent.putExtra("force-locked", DeviceAdminInfo.USES_POLICY_FORCE_LOCK);

            startActivityForResult(intent, 1);
            System.out.println("The Device Could not lock because device admin not enabled");
            //mDPM.lockNow();
        }
        else
        {
            System.out.println("The Device  device admin enabled");
             intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
              intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"onEnabled");
            mDPM.lockNow();
            mDPM.setMaximumTimeToLock(mAdminName, 0);
              intent.putExtra("force-locked", DeviceAdminInfo.USES_POLICY_FORCE_LOCK);

                startActivityForResult(intent, 1);

        }   

希望这对您有所帮助。

于 2012-07-27T01:54:24.130 回答