I want to turn off / lock the screen of my device programmatically.
For the moment, when I try :
DevicePolicyManager mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
mDPM.lockNow();
I have this error :
java.lang.SecurityException: No active admin owned by uid 10176 for policy #3
This is my AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="opteamit.com.belami" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature android:name="android.hardware.location.gps" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application...
What is wrong ?
Well something with high necessity can't finished with two lines of code, lock off screen required device admin. you may follow this :
private void lock() {
PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
if (pm.isScreenOn()) {
DevicePolicyManager policy = (DevicePolicyManager)
getSystemService(Context.DEVICE_POLICY_SERVICE);
try {
policy.lockNow();
} catch (SecurityException ex) {
Toast.makeText(
this,
"must enable device administrator",
Toast.LENGTH_LONG).show();
ComponentName admin = new ComponentName(context, AdminReceiver.class);
Intent intent = new Intent(
DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN).putExtra(
DevicePolicyManager.EXTRA_DEVICE_ADMIN, admin);
context.startActivity(intent);
}
}
}
and AdminReceiverClass:
public class AdminReceiver extends DeviceAdminReceiver {
public static final String ACTION_DISABLED = "device_admin_action_disabled";
public static final String ACTION_ENABLED = "device_admin_action_enabled";
#Override
public void onDisabled(Context context, Intent intent) {
super.onDisabled(context, intent);
LocalBroadcastManager.getInstance(context).sendBroadcast(
new Intent(ACTION_DISABLED));
}
#Override
public void onEnabled(Context context, Intent intent) {
super.onEnabled(context, intent);
LocalBroadcastManager.getInstance(context).sendBroadcast(
new Intent(ACTION_ENABLED));
}
}
also we need declares the security policies used in metadata so for examples with Path android:resource="#xml/device_admin_sample" :
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<limit-password />
<watch-login />
<reset-password />
<force-lock />
<wipe-data />
<expire-password />
<encrypted-storage />
<disable-camera />
</uses-policies>
</device-admin>
in our case we just need :
<?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>`
Now declare it in our manifist.xml :
<receiver
android:name=".AdminReceiver"
android:label="#string/device_admin"
android:description="#string/device_admin_description"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="#xml/device_admin_sample" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>`
Hope it will Help you.
See user6490462 answer below.
private void lock() {
PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
if (pm.isScreenOn()) {
DevicePolicyManager policy = (DevicePolicyManager)
getSystemService(Context.DEVICE_POLICY_SERVICE);
try {
policy.lockNow();
} catch (SecurityException ex) {
Toast.makeText(
this,
"must enable device administrator",
Toast.LENGTH_LONG).show();
ComponentName admin = new ComponentName(context, AdminReceiver.class);
Intent intent = new Intent(
DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN).putExtra(
DevicePolicyManager.EXTRA_DEVICE_ADMIN, admin);
context.startActivity(intent);
}
}
}
For those of us who landed here from google and weren't able to get the admin request to popup, I ended up solving it by making sure that I started the activity from a currently running Activity (thus, calls would not need the context. in front of them. While some may have had luck with intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) from a service, I had to move the call into an Activity.
ComponentName admin = new ComponentName(context, AdminReceiver.class);
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN)
.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, admin);
startActivity(intent);
This worked for me after following user6490462's instructions but I had to tweak the Intent slightly
Even with these changes, for some reason I could not get the activity window to pop up to approve the device admin privilege's. But when I went into Biometrics and security -> Other security settings -> Device admin apps I could see my app there waiting to have its admin privilege's approved.
ComponentName admin = new ComponentName(getApplicationContext(), AdminReceiver.class);
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, admin);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"Allow this app to turn screen off");
startActivity(intent);
Related
I am trying to build and led flash light widget but compiler is not showing any error whenever I try to press widget there show widget loading problem
and android studio does not show any error I am beginner so please help me. Thanks in advance
Meanifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mateen.flash_light_widget">
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<receiver android:name=".NewAppWidget" android:icon="#drawable/example_appwidget_preview" android:label="#string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="#xml/new_app_widget_info" />
</receiver>
<receiver android:name="FlashlightWidgetReceiver">
<intent-filter>
<action android:name="COM_FLASHLIGHT"></action>
</intent-filter>
</receiver>
</application>
</manifest>
appwidgetprovider class
public class NewAppWidget extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Intent receiver = new Intent(context, FlashlightWidgetReceiver.class);
receiver.setAction("COM_FLASHLIGHT");
receiver.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, receiver, 0);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.new_app_widget);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, views);
}
}
recevier there is error I think compiler is also not showing any error
public class FlashlightWidgetReceiver extends BroadcastReceiver {
private static boolean isLightOn = false;
private static Camera camera;
#Override
public void onReceive(Context context, Intent intent) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
if(isLightOn) {
views.setImageViewResource(R.id.button, R.drawable.example_appwidget_preview);
} else {
views.setImageViewResource(R.id.button, R.drawable.example_appwidget_preview);
}
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
appWidgetManager.updateAppWidget(new ComponentName(context, NewAppWidget.class),
views);
if (isLightOn) {
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
isLightOn = false;
}
} else {
// Open the default i.e. the first rear facing camera.
camera = Camera.open();
if(camera == null) {
Toast.makeText(context,"noCamera", Toast.LENGTH_SHORT).show();
} else {
// Set the torch flash mode
Camera.Parameters param = camera.getParameters();
param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
try {
camera.setParameters(param);
camera.startPreview();
isLightOn = true;
} catch (Exception e) {
Toast.makeText(context,"no flash", Toast.LENGTH_SHORT).show();
}
}
}
}
}
layout file for widget
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="#layout/new_app_widget"
android:initialLayout="#layout/new_app_widget"
android:minHeight="40dp"
android:minWidth="40dp"
android:previewImage="#drawable/example_appwidget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen|keyguard"></appwidget-provider>
add these permissions to your manifest
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera.flash" />
i found solution In order to work, the camera needs a surface to turn on.
so after camera.startPreview() i wrote this piece of lines
surfaceTexture = new SurfaceTexture(0);
camera.setPreviewTexture(surfaceTexture);
it solved my problem thanks alot :)
please help . . I build app, but i want to when user try to uninstall mp app, it's require password. I try this but it doesn't work:
In myActivity:
AdminReceiver a = new AdminReceiver();
a.onDisableRequested(getApplicationContext(), getIntent());
AdminReceiver :
public class AdminReceiver extends DeviceAdminReceiver{
static DevicePolicyManager dpm;
static ComponentName devAdminReceiver;
public CharSequence onDisableRequested(final Context context, Intent intent) {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startMain); //switch to the home screen, not totally necessary
lockPhone(context, "pass");
//Log.i(TAG, "DEVICE ADMINISTRATION DISABLE REQUESTED & LOCKED PHONE");
return "haha. i locked your phone.";
}
public static boolean lockPhone(Context context, String password){
devAdminReceiver = new ComponentName(context, AdminReceiver.class);
dpm = (DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE);
boolean pwChange = dpm.resetPassword(password, 0);
dpm.lockNow();
return pwChange;
}
And in AndroidManifest:
<receiver android:name=".app.AdminReceiver"
android:label="#string/app_name"
android:description="#string/hello_world"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="#xml/deviceadmin" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
and last in xml/deviceadmin:
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<limit-password />
<watch-login />
<reset-password />
<force-lock />
<wipe-data />
</uses-policies>
</device-admin>
actually i really confused, how it works. Please help me i really need it for my final project
That is not possible in Android without firmware modifications.
Refer AOSP code for further managing policy
OR
IN AndroidL push deviceower.xml which will make ur app device owner hence u can apply the policy but prerequisites is device should be rroted
Hello I want to make a simple notification application.
I am using this (http://www.youtube.com/watch?v=rmzv716SYkQ) tutorial.
here is my codes.
manifest file
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission
android:name="com.myapp.ntfapp.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.turk.bakistik.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
manifest file in application block
<receiver
android:name=".MyNotificationService"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.myapp.ntfapp" />
</intent-filter>
</receiver>
MyNotificationService Class
public class MyNotificationService extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
try {
String action = intent.getAction();
if (action.equals("com.google.android.c2dm.intent.REGISTRATION")) {
String registrationId = intent.getStringExtra("registration_id");
Log.i("ui", registrationId);
String error = intent.getStringExtra("error");
String unregistered = intent.getStringExtra("unregistered");
}
else if (action.equals("com.google.android.c2dm.intent.RECEIVE")) {
String data1 = intent.getStringExtra("data1");
String data2 = intent.getStringExtra("data2");
}
} finally { }
}
}
Activate and Deactivate buttons in main activity
button2=(Button)findViewById(R.id.button2);
button3=(Button)findViewById(R.id.button3);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
registrationIntent.putExtra("app", PendingIntent.getBroadcast(v.getContext(), 0, new Intent(), 0));
registrationIntent.putExtra("sender", "123456789101112");
startService(registrationIntent);
}
});
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent unregIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER");
unregIntent.putExtra("app", PendingIntent.getBroadcast(v.getContext(), 0, new Intent(), 0));
startService(unregIntent);
}
});
When I click activate button
W/ActivityManager(70): Unable to start service Intent { act=com.google.android.c2dm.intent.REGISTRATION (has extras) }: not found
How can I fix this problem?
I see an inconsistency in your manifest :
<permission
android:name="com.myapp.ntfapp.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.turk.bakistik.permission.C2D_MESSAGE" />
You should use your app's package name in both (either com.myapp.ntfapp
or com.turk.bakistik).
In addition, you are using an old and obsolete way to register to GCM. You should use the GoogleCloudMessaging.register method which is part of the Google Play Services library.
My android app never receives GCM messages on 2.3 devices, but it does on 4.x devices. I can register all devices (2.3 and 4.x) successfully. I thought it might have something to do with this issue, but it seems like I have my android manifest configured properly. Would anyone be able to eyeball my IntentService and BroadcastReceiver and see if they notice any problems? Any help would be greatly appreciated. Note that onHandeIntent() is never called for Android 2.3 when sending notifications while I have the debugger attached. I checked 4.x devices, and they do trigger the debugger in onHandleIntent().
Thanks!
Android Manfest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.package"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="my.package.matchtracker.permission.C2D_MESSAGE" />
<permission android:name="my.package.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="my.package" />
</intent-filter>
</receiver>
<service android:name=".NotificationIntentService" android:enabled="true" />
<activity android:name="com.gigya.socialize.android.GSWebViewActivity" />
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:configChanges="orientation|screenSize"
android:theme="#android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Broadcast Receiver:
package my.package;
import android.app.*;
import android.content.*;
public class GcmBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationIntentService.runIntentInService(context, intent);
setResultCode(Activity.RESULT_OK);
}
}
Notification Intent Service
public class NotificationIntentService extends IntentService {
private String TAG = "NotificationIntentService";
public NotificationIntentService() {
super(AppConstants.GCM_SENDER_ID);
}
public NotificationIntentService(String name) {
super(name);
// TODO Auto-generated constructor stub
}
private static PowerManager.WakeLock sWakeLock;
private static final Object LOCK = NotificationIntentService.class;
static void runIntentInService(Context context, Intent intent) {
synchronized(LOCK) {
if (sWakeLock == null) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
sWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "my_wakelock");
}
}
sWakeLock.acquire();
intent.setClassName(context, NotificationIntentService.class.getName());
context.startService(intent);
}
public final void onHandleIntent(Intent intent) {
try {
String action = intent.getAction();
if (action.equals("com.google.android.c2dm.intent.REGISTRATION")) {
//don't care.
} else if (action.equals("com.google.android.c2dm.intent.RECEIVE")) {
handleMessage(intent);
}
} finally {
synchronized(LOCK) {
sWakeLock.release();
}
}
}
private void handleMessage(Intent intent) {
Bundle b = intent.getExtras();
String text = b.getString("text"),
title = b.getString("title"),
largeImageUrl = b.getString("largeImageUrl");
Log.i(TAG, "Message is " + text);
NotificationManager nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Bitmap bit=null;
if (largeImageUrl != null && !largeImageUrl.isEmpty()) {
try{bit = BitmapFactory.decodeStream((InputStream)new URL(largeImageUrl).getContent());
} catch (Exception e){}
}
NotificationCompat.Builder nc = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.ic_launcher)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setAutoCancel(true) //notification disappears when clicked
.setContentIntent(PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT));
//bit = Bitmap.createScaledBitmap(bit, android.R.dimen.notification_large_icon_width, android.R.dimen.notification_large_icon_height, true);
if (bit != null) nc.setLargeIcon(bit);
nm.notify(0, nc.build());
}
}
The first potential problem I can see is this :
The package name in the permission element is not the same as the one in the uses-permission element. In my app (which targets Android 2.2) they are identical.
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="my.package.matchtracker.permission.C2D_MESSAGE" />
<permission android:name="my.package.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
If you are using product flavor with different applicationId, see my answer to the question GCM messages are not received on android 2.3.6 v but working fine on android 4.X v
GCM need google play services installed on device but in version 2.3 it's not installed by default.
for Lock Screen programmatically using Android. I got some good ideas from Stackoverflow previous questions regarding this, and I did some thing well, but when I run that code, there is No Exception and Error. but also , the screen didn't lock. kindly some one point my fault in this code
My manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.top"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".LockActivity">
</activity>
<activity
android:name=".LockActivity$Myclass"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".LockActivity"
android:label="device_admin"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="#xml/my_admin" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
</manifest>
my Class file
package net.top;
import android.app.Activity;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.widget.Toast;
public class LockActivity extends DeviceAdminReceiver{
public void onEnabled(Context context, Intent intent) {
showToast(context, "Sample Device Admin: enabled");
}
#Override
public CharSequence onDisableRequested(Context context, Intent intent) {
return "This is an optional message to warn the user about disabling.";
}
#Override
public void onDisabled(Context context, Intent intent) {
showToast(context, "Sample Device Admin: disabled");
}
void showToast(Context context, CharSequence msg) {
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
public static class Myclass extends Activity {
protected PowerManager.WakeLock mWakeLock;
DevicePolicyManager mDPM;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
// mDPM.lockNow();
ComponentName mAdminName = new ComponentName(Myclass.this,LockActivity.class);
if(!mDPM.isAdminActive(mAdminName))
{
Intent intent = new
Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
mAdminName);
//mDPM.lockNow();
}
else
{
mDPM.lockNow();
}
}
}
}
I also added the My_admin.xml file
<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<limit-password />
<watch-login />
<reset-password />
<force-lock />
<wipe-data />
<expire-password />
<encrypted-storage />
<disable-camera />
</uses-policies>
</device-admin>
I can't find what is goes wrong here. kindly help me to point out my mistake here. thanks in advance.
you can add this code into your Activity class, it's works fine for me
if(!mDPM.isAdminActive(mAdminName))
{
intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"is locked");
intent.putExtra("force-locked", DeviceAdminInfo.USES_POLICY_FORCE_LOCK);
startActivityForResult(intent, 1);
System.out.println("The Device Could not lock because device admin not enabled");
//mDPM.lockNow();
}
else
{
System.out.println("The Device device admin enabled");
intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"onEnabled");
mDPM.lockNow();
mDPM.setMaximumTimeToLock(mAdminName, 0);
intent.putExtra("force-locked", DeviceAdminInfo.USES_POLICY_FORCE_LOCK);
startActivityForResult(intent, 1);
}
Hope this would be helpful for you.