android - 如何让设备在代码中进入休眠状态?

标签 android

我在网上搜索了如何强制设备关闭屏幕(模拟电源按键是一个选项),但几乎没有发现任何信息。

我正在尝试构建一个锁屏应用程序,所以我想点击一个按钮来打开 kiosk 模式(这样我的锁屏就可以运行,一旦通过身份验证它就会自行关闭)并在 kiosk 模式之后已打开我想让设备休眠。

有什么办法吗?

现在我有我的锁屏应用程序和信息亭模式的原型(prototype)我想在 onCreate 方法的这部分代码中添加关闭屏幕选项:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_demo);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    findViewById(R.id.next).setOnClickListener(this);
    setUpKioskMode();

}


    private void setUpKioskMode() {
    if (!MySharedPreferences.isAppLaunched(this)) {
        Log.d(TAG, "onCreate() locking the app first time");
        kioskMode.lockUnlock(this, true);
        MySharedPreferences.saveAppLaunched(this, true);
    } else {
        //check if app was locked
        if (MySharedPreferences.isAppInKioskMode(this)) {
            Log.d(TAG, "onCreate() locking the app");
            kioskMode.lockUnlock(this, true);
        }
    }
}

最佳答案

要锁定设备屏幕,您需要探索设备管理 API,让用户直接在应用程序内锁定设备屏幕。

您可以按照以下步骤来实现这一点。

  1. res/xml/policies.xml 中创建一个 policies.xml>

policies.xml

    <?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-policies>
        <force-lock />
    </uses-policies>
</device-admin>
  1. 将此添加到 application 标记内的 Manifest.xml 中。

        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
        </intent-filter>
    
    </receiver>    
    
  2. 创建一个 MyAdmin 类,它是 DeviceAdminReceiver 类的子类。

    public class MyAdmin extends DeviceAdminReceiver {
    
    @Override
    public void onEnabled(Context context, Intent intent) {
        Toast.makeText(context, "Device Admin : enabled", Toast.LENGTH_SHORT).show();
    }
    
    @Override
    public void onDisabled(Context context, Intent intent) {
        Toast.makeText(context, "Device Admin : disabled", Toast.LENGTH_SHORT).show();
    }    }
    
  3. 在您的 Activity 中:

private DevicePolicyManager devicePolicyManager;

 private ComponentName compName;

//initialize in onCreate 
  devicePolicyManager=(DevicePolicyManager)getSystemService(DEVICE_POLICY_SERVICE);
           compName=new ComponentName(this,MyAdmin.class);
       //initialize in Oncreate 
        devicePolicyManager=DevicePolicyManager)getSystemService(DEVICE_POLICY_SERVICE);
       compName=new ComponentName(this,MyAdmin.class);

@onClick Button
boolean active=devicePolicyManager.isAdminActive(compName);

        if(active){
        devicePolicyManager.lockNow();
        }else{
        Toast.makeText(this,"You need to enable the Admin Device Features",Toast.LENGTH_SHORT).show();
        }
  1. 请求用户许可:

    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);      intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, compName);      intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Additional text explaining why we need this permission");startActivityForResult(intent, RESULT_ENABLE);
    

检查结果

   @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode) {
      case RESULT_ENABLE :
        if (resultCode == Activity.RESULT_OK) {
          Toast.makeText(MainActivity.this, "You have enabled the Admin Device features", Toast.LENGTH_SHORT).show();
        } else {
          Toast.makeText(MainActivity.this, "Problem to enable the Admin Device features", Toast.LENGTH_SHORT).show();
        }
      break;
    }

    super.onActivityResult(requestCode, resultCode, data);
  }
}

Here是相同的完整实现。

关于android - 如何让设备在代码中进入休眠状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54745006/

相关文章:

android - 错误: Unknown Issue with Google Play Services

android - "No such file or directory"试图在 Android 设备上执行 linux 二进制文件

android - Android Widget ID 是否持久化

android - Libpng 漏洞

android - 通过另一个适配器访问一个适配器的数据

java - 生产 Android 应用显示 Caused by : java. lang.NoClassDefFoundError 错误

android - Android:在不同的 Activity 中播放很多短音轨

android - 有没有办法为 ndk-build 的输出着色?

android - Qt5 (c++) android监听系统按钮

android - 查找按钮的父对话框