How do i lock phone programmatically android

喜欢而已 提交于 2019-11-27 02:25:29

问题


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

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)

Any help is appreciated


回答1:


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

Create a new empty project and create a class called MyAdminReceiver that extends DeviceAdminReceiver like this

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.xml and add the policies, in you case its locking the screen

<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

<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.java add code like this

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_TASK but when you use that your window might not pop like in your case so Try opening it from a subclass of an activity only

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




回答2:


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

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

Specifically:

  • You need to have a BroadcastReceiver in 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

Here is the developer documentation on the device admin APIs.



来源:https://stackoverflow.com/questions/19745890/how-do-i-lock-phone-programmatically-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!