
- Android Basics
- Android - Home
- Android - Overview
- Android - Environment Setup
- Android - Architecture
- Android - Application Components
- Android - Hello World Example
- Android - Resources
- Android - Activities
- Android - Services
- Android - Broadcast Receivers
- Android - Content Providers
- Android - Fragments
- Android - Intents/Filters
- Android - User Interface
- Android - UI Layouts
- Android - UI Controls
- Android - Event Handling
- Android - Styles and Themes
- Android - Custom Components
- Android Advanced Concepts
- Android - Drag and Drop
- Android - Notifications
- Location Based Services
- Android - Sending Email
- Android - Sending SMS
- Android - Phone Calls
- Publishing Android Application
- Android Useful Examples
- Android - Alert Dialoges
- Android - Animations
- Android - Audio Capture
- Android - AudioManager
- Android - Auto Complete
- Android - Best Practices
- Android - Bluetooth
- Android - Camera
- Android - Clipboard
- Android - Custom Fonts
- Android - Data Backup
- Android - Developer Tools
- Android - Emulator
- Android - Facebook Integration
- Android - Gestures
- Android - Google Maps
- Android - Image Effects
- Android - ImageSwitcher
- Android - Internal Storage
- Android - JetPlayer
- Android - JSON Parser
- Android - Linkedin Integration
- Android - Loading Spinner
- Android - Localization
- Android - Login Screen
- Android - MediaPlayer
- Android - Multitouch
- Android - Navigation
- Android - Network Connection
- Android - NFC Guide
- Android - PHP/MySQL
- Android - Progress Circle
- Android - ProgressBar
- Android - Push Notification
- Android - RenderScript
- Android - RSS Reader
- Android - Screen Cast
- Android - SDK Manager
- Android - Sensors
- Android - Session Management
- Android - Shared Preferences
- Android - SIP Protocol
- Android - Spelling Checker
- Android - SQLite Database
- Android - Support Library
- Android - Testing
- Android - Text to Speech
- Android - TextureView
- Android - Twitter Integration
- Android - UI Design
- Android - UI Patterns
- Android - UI Testing
- Android - WebView Layout
- Android - Wi-Fi
- Android - Widgets
- Android - XML Parsers
- Android Useful Resources
- Android - Questions and Answers
- Android - Useful Resources
- Android - Discussion
How to lock the Android device programmatically?
This example demonstrate about How to lock the Android device programmatically.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml
<? xml version= "1.0" encoding= "utf-8" ?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android :layout_width= "match_parent" android :layout_height= "match_parent" android :layout_margin= "16dp" tools :context= ".MainActivity" > <LinearLayout android :layout_width= "match_parent" android :layout_height= "wrap_content" android :layout_centerInParent= "true" android :orientation= "horizontal" > <Button android :id= "@+id/btnEnable" android :layout_width= "0dp" android :layout_height= "wrap_content" android :layout_weight= "1" android :onClick= "enablePhone" android :text= "Enable" /> <Button android :id= "@+id/btnLock" android :layout_width= "0dp" android :layout_height= "wrap_content" android :layout_weight= "1" android :onClick= "lockPhone" android :text= "Lock" /> </LinearLayout> </RelativeLayout>
Step 3 − Add the following code to res/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>
Step 4 − Add the following code to src/DeviceAdmin
package app.tutorialspoint.com.sample ; import android.app.admin.DeviceAdminReceiver ; import android.content.Context ; import android.content.Intent ; import android.widget.Toast ; public class DeviceAdmin extends DeviceAdminReceiver { @Override public void onEnabled (Context context , Intent intent) { super .onEnabled(context , intent) ; Toast. makeText (context , "Enabled" , Toast. LENGTH_SHORT ).show() ; } @Override public void onDisabled (Context context , Intent intent) { super .onDisabled(context , intent) ; Toast. makeText (context , "Disabled" , Toast. LENGTH_SHORT ).show() ; } }
Step 5 − Add the following code to src/MainActivity
package app.tutorialspoint.com.sample ; import android.app.Activity ; import android.app.admin.DevicePolicyManager ; import android.content.ComponentName ; import android.content.Context ; import android.content.Intent ; import android.support.annotation. Nullable ; import android.support.v7.app.AppCompatActivity ; import android.os.Bundle ; import android.view.View ; import android.widget.Button ; import android.widget.Toast ; public class MainActivity extends AppCompatActivity { static final int RESULT_ENABLE = 1 ; DevicePolicyManager deviceManger ; ComponentName compName ; Button btnEnable , btnLock ; @Override protected void onCreate (Bundle savedInstanceState) { super .onCreate(savedInstanceState) ; setContentView(R.layout. activity_main ) ; btnEnable = findViewById(R.id. btnEnable ) ; btnLock = findViewById(R.id. btnLock ) ; deviceManger = (DevicePolicyManager) getSystemService(Context. DEVICE_POLICY_SERVICE ) ; compName = new ComponentName( this, DeviceAdmin. class ) ; boolean active = deviceManger .isAdminActive( compName ) ; if (active) { btnEnable .setText( "Disable" ) ; btnLock .setVisibility(View. VISIBLE ) ; } else { btnEnable .setText( "Enable" ) ; btnLock .setVisibility(View. GONE ) ; } } public void enablePhone (View view) { boolean active = deviceManger .isAdminActive( compName ) ; if (active) { deviceManger .removeActiveAdmin( compName ) ; btnEnable .setText( "Enable" ) ; btnLock .setVisibility(View. GONE ) ; } else { Intent intent = new Intent(DevicePolicyManager. ACTION_ADD_DEVICE_ADMIN ) ; intent.putExtra(DevicePolicyManager. EXTRA_DEVICE_ADMIN , compName ) ; intent.putExtra(DevicePolicyManager. EXTRA_ADD_EXPLANATION , "You should enable the app!" ) ; startActivityForResult(intent , RESULT_ENABLE ) ; } } public void lockPhone (View view) { deviceManger .lockNow() ; } @Override protected void onActivityResult ( int requestCode , int resultCode , @Nullable Intent data) { super .onActivityResult(requestCode , resultCode , data) ; switch (requestCode) { case RESULT_ENABLE : if (resultCode == Activity. RESULT_OK ) { btnEnable .setText( "Disable" ) ; btnLock .setVisibility(View. VISIBLE ) ; } else { Toast. makeText (getApplicationContext() , "Failed!" , Toast. LENGTH_SHORT ).show() ; } return; } } }
Step 6 − Add the following code to androidManifest.xml
<? xml version= "1.0" encoding= "utf-8" ?> <manifest xmlns: android = "http://schemas.android.com/apk/res/android" package= "app.tutorialspoint.com.sample" > <uses-permission android :name= "android.permission.CALL_PHONE" /> <application android :allowBackup= "true" android :icon= "@mipmap/ic_launcher" android :label= "@string/app_name" android :roundIcon= "@mipmap/ic_launcher_round" android :supportsRtl= "true" android :theme= "@style/AppTheme" > <activity android :name= ".MainActivity" > <intent-filter> <action android :name= "android.intent.action.MAIN" /> <category android :name= "android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android :name= ".DeviceAdmin" android :description= "@string/app_description" android :label= "@string/app_name" android :permission= "android.permission.BIND_DEVICE_ADMIN" > <meta-data android :name= "android.app.device_admin" android :resource= "@xml/policies" /> <intent-filter> <action android :name= "android.app.action.DEVICE_ADMIN_ENABLED" /> </intent-filter> </receiver> </application> </manifest>
Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen –
- Related Articles
- How to lock & unlock the iOS device programmatically
- How to get programmatically android device name?
- How to get programmatically android device brand name?
- How to get programmatically android device broad name?
- How to get programmatically android device secure id?
- How to make an Android device vibrate programmatically?
- How to get the IP address of android device programmatically?
- How to lock screen orientation on all android devices programmatically?
- How to get programmatically android device finger print information?
- How to programmatically turn on Wifi on Android device?
- How to turn Android device screen on and off programmatically?
- How to make an Android device vibrate programmatically using Kotlin?
- How to get the IP address of the Android device programmatically using Kotlin?
- How to turn Android device screen on and off programmatically using Kotlin?
- How can I read SMS messages from the device programmatically in Android?
