How to lock the phone programmatically

Ho, am I programmatically closing my Android phone? I tried to run this example. But when I press the power button, the activity appears for a few milliseconds, and then automatically closes

There are no errors in the log, only this log

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

Can someone tell me how to lock the Android screen (like lock, when you make many attempts to lock the picture and lock the phone)

Any help is appreciated

+6
source share
2 answers

You need to make your application as admin, read something here

Create a new empty project and create a class called MyAdminReceiver that extends DeviceAdminReceiver as follows

 import android.app.admin.DeviceAdminReceiver; public class MyAdminReceiver extends DeviceAdminReceiver{ } 

Create a new folder called xml and create an XML file for your administrator rights called admin.xml and add policies in case you lock the screen

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

In the manifest, add the receiver to the 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 Intent for Admin Device other, which is from the Activity subclass, there is a possibility that you may get an error to use Intent.FLAG_ACTIVITY_NEW_TASK , but when you use this, your window may not pop, as in your case, so try opening its from a subclass only activity

Also, you cannot delete the application if it was not registered as an administrator

+19
source

But when I press the power button, the activity appears for a few milliseconds, and then automatically closes

The code shown in this example is called up by the Settings app when clicked.

Can someone tell me how to lock android screen

You are using the code you are associated with. Here is an example of my application showing the same basic thing.

In particular:

  • The manifest must have BroadcastReceiver , which is configured as a component of the device administrator.

  • User must activate your application as device administrator

  • Then you call lockNow() on DevicePolicyManager

Here is the developer documentation on the device APIs.

0
source

Source: https://habr.com/ru/post/957257/


All Articles