How to display an activity over the lock screen? - android

I know an activity will get killed when the screen is locked. So wanna use service i guess. how to display an activity or ring a sound over the lock screen by pressing power button twice? I tried many questions related to it here but i don't find solution at all.
Here is the question I posted before.
https://stackoverflow.com/questions/28242776/toast-is-not-displayed-over-the-lock-screen

to show your Activity on lock screen you need to register reciever for off/on screen, then in onReceive() method start your activity on lock screen and keep screen on and ... .
see this Link . it should help you.

call this method at your Activity onCreate()
public void aboveLockScreen() {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
}

Related

Unable to start call Activity while device is lock

I'm trying to open the activity on lock screen when I receive the notification form twilio. But I'm facing issue as it called onDestroy method when screen is locked on call receiving and it doesn't open the required activity.
I tried many solution but none of them worked.
I also tried following code to open activity on Lock screen but it doesn't work.
Window window=getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);

Android : Screen is not waking up

I am working on calling feature and i want an activity to be opened and the screen to wake up whenever there is an incoming call. I am able to start the activity but am not able to wake up the screen. I have tried as many solutions available here on Stackoverflow, still no luck.
I am providing the links of few of the solutions i have tried..
Turning on screen programmatically
Light up screen when notification received android
Can anyone help me out with how can I achieve this?
Add this to activity you want to open when screen is locked:
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
}
Could you please provide a bit more information (or post the screen activity fragment ideally) from what i understand reading the 2 solutions you included
you have not set the flags at all for the windowmanager :
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
the full wake lock method is deprecated and it is likely to kill the activity

Android FLAG_SHOW_WHEN_LOCKED when invoking activity from history

I've been trying to use FLAG_SHOW_WHEN_LOCKED and FLAG_DISMISS_KEYGUARD to display an activity each time someone turn the screen on. It works well most of the time but there is some case causing strange behaviour. This happens when the activity is already existing and I send it a intent after it has been moved back in the history stack, by using the Home button.
First, if I press the Home button when on my activity, then switch the screen off and on, the activity is expected to be displayed right away as it was. However what is displayed instead is the basic android lockscreen. My activity is behind.
Worst, sometime, if I touch the home screen, then turn the screen off and on, the home screen overlay (all appli logo and widgets) are displayed with my activity in the background!
The intent I am using each time the screen is turned off to laucn and recall the activity :
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
context.startActivity(intent);
So, it seems that when my activity is in the history stack, it sometimes does not correctly get recalled and bypass the android lockscreen. Is there something wrong with my intent?
Add more flags :
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
Wither via Intent.addFlags() or using getWindow().addFlags() from your activity. These flag will disable lockscreen when your activity is started
This will solve your query
Reference Answer : Here
You can integrate the Adenda SDK in your app to achieve what you're after. They let you display what you want on the lock screen relatively easily.
I'm affiliated with Adenda, for full disclosure

Start activity when screen is off

I have set up an AlarmManager to start up an activity. This activity also plays a sound, similar to an alarm app or an incoming call.
It works ok if the screen is on, even if the screen is locked.
If the screen is off, it doesn't work at all. I tried using the following as the first thing in onCreate
getWindow().setFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON, WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
If the screenlock is not enabled, this turns on the screen and I can see my activity closing. I can't hear the sound playing. If the screenlock is enabled, the screen won't turn on at all.
Sometimes I get the following, but not always:
07-18 23:52:13.685: E/OpenGLRenderer(14148): GL_INVALID_OPERATION
How can I make it start properly when the screen is off?
I got my answer partially from here.
lock = ((KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE)).newKeyguardLock(KEYGUARD_SERVICE);
powerManager = ((PowerManager) getSystemService(Context.POWER_SERVICE));
wake = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
lock.disableKeyguard();
wake.acquire();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
A while back I read that your app must be in full screen for the FLAG_TURN_SCREEN_ON to work.
"** One important note. Your activity must be full screen in order for the above flag combination to work. In my app I tried to use these flags with an activity which is not full screen (Dialog Theme) and it didn't work. After looking at the documentation I found that these flags require the window to be a full screen window." -Wake Android Device up
Quote from someone who posted their about a similar issue with FLAG_X.
Look into running a service, activity is going to be stopped when not in foreground.
Also look into the Activity lifecycle. http://developer.android.com/reference/android/app/Activity.html

Starting activity from service on lock screen turns on the screen but does not show the activity itself

I'm trying to start an activity from a service I had already acquired the lock for as follows:
Intent i = new Intent(context, MyActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
startActivity(i);
The activity manifest is declared as follows:
<activity
android:name=".MyActivity"
android:configChanges="orientation|screenSize|keyboardHidden|keyboard|navigation"
android:excludeFromRecents="true"
android:launchMode="singleInstance"
android:screenOrientation="nosensor"
android:showOnLockScreen="true"
android:taskAffinity=""
android:theme="#style/MyTheme" />
And finally, on onCreate() or on onAttachedToWindow() (I tried on both), I add the following flags:
final Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
The problem is that the first time I call startActivity() from my service, the screen turns on but the activity itself does not show up. It shows the lock screen instead. Every subsequent call of startActivity() works properly but I can't find a reason for this odd behavior.
I tried already suggestions to get a full wakelock instead of partial, change the flags and values in the manifest according to the following SO answers:
Android Activity Not Showing When Screen is Woken Up and Lock Screen Not Disabling
how to unlock the screen when BroadcastReceiver is called?
Programmatically turn screen on in android
Android Galaxy S4 -- Activity that is visible over lock screen
Note that my theme is not a dialog but a fullscreen activity.
Any other ideas?
I'm facing the same problem, after a lot of searching here and google, found this which unlocked the screen and popped my activity but it only works for me when the app is running (foreground/background).
import android.view.Window;
import android.view.WindowManager.LayoutParams;
Window window = this.getWindow();
window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
i'm trying to start an activty when app is closed... (using broadcast receiver)
in the docs (for example here) and most of the answers on SO the flags are added this way:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
but when i tried the way it is like in the example it unlocked the screen instead of just turning on the screen.
hope this help . it still didn't solve my problem completely.
EDIT:
found this post which solved my problem.
there is a comment there on NOT using a dialog theme which solved it for me
Step 1: Add below code in your activity before
setContentView(R.layout.activity_about_us);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
Step 2: Lock your mobile than you will see activity in which you have added this code.
You can implement this if you want to open particular screen by notification occurrence like skype call.
Since my application already includes a Service, this is what I do: if the screen is locked, I register a broadcast receiver (a bit simpler than this one, since it reacts only on unlocking) that starts the Activity as soon as the screen gets unlocked.

Categories

Resources