gpt4 book ai didi

android - Android 的锁屏无法使用 Android 正常工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:06:56 31 4
gpt4 key购买 nike

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

我的类(class)文件

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>

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

最佳答案

您可以将此代码添加到您的 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);

}

希望对您有所帮助。

关于android - Android 的锁屏无法使用 Android 正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11680291/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com