Activate Device Admin in android [duplicate] - android

I am looking for a way to replace the stock lock screen (with an app, not a rom).
What is the best way to do it, for a start to disable the lock screen on as much devices as possible?
Thanks!

KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
in androidmanifest:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>

You can just use this line in the activity:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

Try this, it will keep awake the screen/ display , as long as the activity is on top.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Also this does not require any permission in manifest.

Check out this link http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
listen to the screen on intent and I guess just launch your lock screen.

Related

Wake up device programmatically

I want to wake up and unlock a device. Then, I'd like show an activity when the user has new message from Firebase.
I wrote this on onResume() method:
window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
This works for devices with API > 19. The problem is that in KitKat, it either does nothing or it wakes up the screen but doesn't unlock the device.
Also I set the right permissions in the AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
Also in MyFirebaseMessagingService.class, I'm starting an activity with FLAG_ACTIVITY_NEW_TASK as flag.
Does anyone know what am I missing?
Thanks for the help.
Ok i found it. I just add that piece of code and it worked
KeyguardManager manager = (KeyguardManager) this.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock lock = manager.newKeyguardLock("abc");
lock.disableKeyguard();

Android - Kill an app when screen turned off or screen times out

I want to kill / fully close an app so it doesn’t run even in the background when I press the screen turn on/off button or if the screen times out. I couldn’t find a solution anywhere on the internet. Can you guys help me out with a code snippet? Thanks
you can refer this link to detect screen turn off
Screen off Broadcast receiver and for killing the app you can use below code
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
first check if the screen is locked inside a service that runs in the background:
KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if( myKM.inKeyguardRestrictedInputMode()) {
//it is locked
getActivity().finish();
System.exit(0);
} else {
//it is not locked
}
then you simply kill the app if the screen is locked.
hope this will help.
To make activity like a toast (appear-and-go) add following code into manifest:
<activity android:name=".YourActivity"
android:label="YourActivityLabel"
android:taskAffinity=""
android:clearTaskOnLaunch="true"
android:excludeFromRecents="true"
android:finishOnTaskLaunch="true"
android:noHistory="true"
android:launchMode="singleTask">
</activity>

Android Lollipop - Bypass lock screen for popup activity

I have Activity1 which is a list screen of items. Each item can be viewed in a separate Activity2 which is displayed as a popup. Activity1 can be launched from the background and displayed even when the screen is locked. Activity1 may also choose to automatically display the contents of an item in the list screen by starting Activity2. We can bypass the lock screen because both activities have the WindowManagerFlags.DismissKeyguard enabled in the OnCreate method.
Before Android Lollipop everything worked as expected. But now the popup Activity2 is not visible unless the device is manually unlocked. If I change Activity2 to be a full screen Activity then everything seems to work (Except transitioning from one activity to another will briefly display the lock screen). Any ideas on how to fix this cleanly?
Also, I have only tried the Galaxy S6/S6 Edge devices which have this new Knox security feature on them.
Edit I have changed Activity2 to be a DialogFragment instead of an Activity. This worked for me best because the suggested answer used code that is deprecated or obsolete depending on the target sdk. Activity1 is using the following flags to bypass the lock screen when needed.
getWindow().addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
Since Activity2 is now just a DialogFragment, it uses the window flags of the parent Activity1. I also remove those flags on the "android.intent.action.SCREEN_OFF" action so that the activity bypasses the lock screen only when launched as a notification and not every time the activity is at the top of the stack. Permissions mentioned in the answer are required.
For sure this snippet should help You (works with Lollipop):
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "My Tag");
wl.acquire();
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
keyguardLock = keyguardManager.newKeyguardLock("TAG");
keyguardLock.disableKeyguard();
and when leaving Your activity (i.e. onStop(), onPause() and onDestroy()):
keyguardLock.reenableKeyguard();
Also, don't forget about permissions:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

Turn off the screen in android

I google this and there is a lot of examples in handling the even on_screen_of and on_screen_off, but I want to actually turn on and turn off the screen. Every search I made it lead me on handling the screen on and off event.
I want to have a button that when clicked will turn the screen off.
How can I do this, and what permissions do I need ? or I this is talked before please give me some useful link
Thanks
PowerManager manager = (PowerManager) getSystemService(Context.POWER_SERVICE);
manager.goToSleep(int amountOfTime);
You will probably need this permission too:
<uses-permission android:name="android.permission.WAKE_LOCK" />
Answer extracted from here

Air for Android app is keeping screen awake when not in app

I have an air for android app that has event listeners for ACTIVATE and DEACTIVATE, inside the activate I tell the screen to stay awake and in the deactivate I tell it to go back to normal like so :
stage.addEventListener(Event.DEACTIVATE, deactivateHandler);
stage.addEventListener(Event.ACTIVATE, activateHandler);
protected function deactivateHandler(event:Event):void{
SFX.disableSound();
NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.NORMAL;
}
protected function activateHandler(event:Event):void{
NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
}
But the screen will stay awake at all times even when on the android home screen unless you force close the app... any ideas?
Thanks
I had this exact problem. My app was able to keep the screen on by setting systemIdleMode to SystemIdleMode.KEEP_AWAKE, and it would force the screen to stay on. However, when the app tried to set the systemIdleMode back to SystemIdleMode.NORMAL, so the screen could turn off, the screen was still staying on.
What turned out to be the problem in my case is was a missing android permission. I had already added this permission to my app XML file, so that i could use the keep-alive function:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
Turns out this is not the only permission you need. I added this permission also:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
And suddenly my app was able to let the screen be turned off again.
You could try using this in your java code:
// Gets one of the views visible on the screen and sets keepScreenOn to true.
// This means the screen will stay on as long as the specified view is visible.
this.findViewById(R.id.viewId).setKeepScreenOn(true);
Or you could put android:keepScreenOn="true" in your layout.

Categories

Resources