Java 我如何以编程方式锁定手机 android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19745890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:53:12  来源:igfitidea点击:

How do i lock phone programmatically android

javaandroiddevice-manager

提问by

Ho do i lock my android phone programmatically ? I tried following thisexample. But when i click on the enable button the Activity pops up for few milliseconds and then closes automatically

我如何以编程方式锁定我的 android 手机?我试着按照这个例子。但是当我点击启用按钮时,活动会弹出几毫秒然后自动关闭

The log shows no error just this log

日志仅显示此日志没有错误

 Log.i("DeviceAdminSample", "Admin enable FAILED!");

Can any one tell me how to lock the android screen (Like the lock when make to many attempts in pattern lock and the phone locks down)

谁能告诉我如何锁定android屏幕(就像多次尝试模式锁定而手机锁定时的锁定一样)

Any help is appreciated

任何帮助表示赞赏

采纳答案by Girish Nair

You have to make your app as admin, Read something over here

你必须让你的应用程序成为管理员,在这里阅读一些东西

Create a new empty project and create a class called MyAdminReceiverthat extends DeviceAdminReceiverlike this

创建一个新的空项目并创建一个名为MyAdminReceiver扩展的DeviceAdminReceiver

import android.app.admin.DeviceAdminReceiver;

public class MyAdminReceiver extends DeviceAdminReceiver{

}

Create a new folder called xml and create an .xml file for your admin rights called admin.xmland add the policies, in you case its locking the screen

创建一个名为 xml 的新文件夹并为您的管理员权限创建一个 .xml 文件admin.xml并添加策略,以防它锁定屏幕

<device-admin xmlns:android="http://schemas.android.com/apk/res/android" >
    <uses-policies>
        <force-lock />
    </uses-policies>
</device-admin>

In your manifest add the receiver under Application tag

在您的清单中,在 Application 标签下添加接收器

<receiver
    android:name="MyAdminReceiver"
    android:permission="android.permission.BIND_DEVICE_ADMIN">
    <meta-data
        android:name="android.app.device_admin"
        android:resource="@xml/admin"/>

    <intent-filter>
        <action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
    </intent-filter>
</receiver>

And in your MainActivity.javaadd code like this

在你MainActivity.java添加这样的代码中

import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    private static final int ADMIN_INTENT = 15;
    private static final String description = "Some Description About Your Admin";
    private DevicePolicyManager mDevicePolicyManager; 
    private ComponentName mComponentName;  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDevicePolicyManager = (DevicePolicyManager)getSystemService(  
                  Context.DEVICE_POLICY_SERVICE);  
        mComponentName = new ComponentName(this, MyAdminReceiver.class);  
        Button btnEnableAdmin = (Button) findViewById(R.id.btnEnableAdmin);
        Button btnDisableAdmin = (Button) findViewById(R.id.btnDisableAdmin);
        Button btnLock = (Button) findViewById(R.id.btnLock);
        btnEnableAdmin.setOnClickListener(this);
        btnDisableAdmin.setOnClickListener(this);
        btnLock.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btnEnableAdmin:
            Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mComponentName);
            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,description);
            startActivityForResult(intent, ADMIN_INTENT);
        break;

        case R.id.btnDisableAdmin:
            mDevicePolicyManager.removeActiveAdmin(mComponentName);  
            Toast.makeText(getApplicationContext(), "Admin registration removed", Toast.LENGTH_SHORT).show();
        break;

        case R.id.btnLock:
             boolean isAdmin = mDevicePolicyManager.isAdminActive(mComponentName);  
             if (isAdmin) {  
                 mDevicePolicyManager.lockNow();  
             }else{
                 Toast.makeText(getApplicationContext(), "Not Registered as admin", Toast.LENGTH_SHORT).show();
             }
        break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == ADMIN_INTENT) {
            if (resultCode == RESULT_OK) {
                Toast.makeText(getApplicationContext(), "Registered As Admin", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(getApplicationContext(), "Failed to register as Admin", Toast.LENGTH_SHORT).show();
            }
        }
    }

}

Note:If you try to call the Intent for Admin Device other that from an Activity subclass there are chances you might get an error to use Intent.FLAG_ACTIVITY_NEW_TASKbut when you use that your window might not pop like in your case so Try opening it from a subclass of an activity only

注意:如果您尝试从 Activity 子类中调用其他管理设备的 Intent,则可能会出现使用错误,Intent.FLAG_ACTIVITY_NEW_TASK但是当您使用它时,您的窗口可能不会像您的情况那样弹出,因此请尝试从子类中打开它仅一项活动

Also you cannot un-install your app unless it has not be unregistered as an admin

您也不能卸载您的应用程序,除非它尚未注销为管理员

回答by CommonsWare

But when i click on the enable button the Activity pops up for few milliseconds and then closes automatically

但是当我点击启用按钮时,活动会弹出几毫秒然后自动关闭

The code shown in that sample will bring up the Settings application when clicked.

该示例中显示的代码将在单击时显示设置应用程序。

Can any one tell me how to lock the android screen

谁能告诉我如何锁定android屏幕

You use the code that you linked to. Here is my sample appshowing the same basic thing.

您使用链接到的代码。这是我的示例应用程序,显示了相同的基本内容。

Specifically:

具体来说:

  • You need to have a BroadcastReceiverin your manifest that is set up to be a device admin component

  • The user has to activate your app as a device admin

  • You then call lockNow()on DevicePolicyManager

  • BroadcastReceiver的清单中需要有一个设置为设备管理组件

  • 用户必须以设备管理员身份激活您的应用

  • 然后调用lockNow()DevicePolicyManager

Here is the developer documentationon the device admin APIs.

这是有关设备管理 API的开发人员文档