Android: How To Start An Activity When Screen Locked - android

I'm developing a VOIP liked app and I want to receive a call when my phone is screen locked.
Also, if I set the lock pattern for my phone, I want to show the activity before I make the unlock pattern. Could you please help to give me any advice or example?
Thanks very much.

Try this it will open screen lock
Window wind;
wind = this.getWindow();
wind.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
wind.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);

Thanks, I 'm following your indication and it do resolve my problem, also as below:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
But the call sequence of Activity callback is very strange, it will call onStart(), onResume(), onPause(), onStop(), then recall onStart(), onPause() suddenly. And because I finish() the Activity when onPause(), so I incorrectly believe the Activity is nevery resumed. Thanks again for your help.

Related

Before Android Activity resumes will it always call onResume()

Will my Android Activity always call onResume() when it goes to the foreground?
Yes, based on Activity lifecycle.
First of all, I suggest to look at the Android Activity Life-cycle management.
http://developer.android.com/training/basics/activity-lifecycle/starting.html
The Activity Lifecycle always rest till onResume() method when the screens comes fully visible on the foreground otherwise it may stop at onStart() when the screen view is partially visible possibly blocked by a modal dialog.

keep android screen from dimming while activity is running

For my android app, I want to keep the screen from dimming while certain activities (e.g. CatActivity) are in the resume state. Does anyone know how to do that? is there a way to declare so in the manifest or in the activity itself?
UPDATE
I found the answer:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Is there a way to turn it off? I don't see a removeFlags method.
To delete the flag simply call the clearFlags() method:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

cancel the events of lock/unlock on my android application

When i unlock my device to see my application, the process in onResume() is launched. How do I cancel the events of unlock, to avoid onResume() from processing?
OnResume is always called when your Activity was in background (e.g. other App, Lockscreen, Homescreen... is shown).
Look at the Activity Life Cycle to check if you can move your code from onResume() maybe to onStart() to fix your issue.
To prevent unlock your device use below flag in your activity.
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
Code
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Note :
As stated on the Activity lifecycle docs, onCreate and onResume will always both be called the first time an Activity is started. When going back to Activity, at least onResume will be called

Activity changes when lock screen

[SOLVED]
i got a question... When i lock my phone while my app is running, it changes automatically to the main activity (the one which is started at the beginning) when in unlock it.
Anybody knows how to handle that the activity at the moment of locking stays?
Thanks!
Does your activity call finish() in its onPause() method?If so remove it

Strange lifecycle behavior in my Screen Saver App

I just developed a screen saver app and I found a strange behavior in its lifecycle.
My work flow is like this:
start my RegisterService, where I call registerReceiver method to register a BroadcastReceiver, which can receive ACTION_SCREEN_OFF.
2.In the onReceive method of this BroadcastReceiver, I start an activity as the screensaver.
3.In the activity, I write Log.i() statement to track its running.
My question is:
When the screen times out, or when I press the POWER key, the screen turns off, and the system will send ACTION_SCREEN_OFF message. As I expect, my receiver starts the screen saver activity. However, I find this Activity calls onCreate(), onResume(), onPause(), onResume() sequentially according to the output in logcat.
It seems as if some a activity comes at front of my screensaver and finishes immediately, so my screensaver calls onPause() and then onResume().
Any idea? This problem handicaps me in programming, please help. Thanks!
Well based on a brief study of the PowerManagerService.java source code, when it's time to turn the screen off, the system initiates an animation (look at line 2183 for the class source) to do that. That means that your activity will pause and then will resume after the animation has ended.
I cannot be 100% sure for this, since I haven't tested it in my environment but this is the only logical explanation I found for your situation.
Hope this helps...
I can recommend you something very easy that might work for you, if you do not want the pause behavior why don't you try to Override the method onPause() and just do nothing :P don't call super.onPause() and that will terminate the behavior of it.
Other thing that might work for you, declare a static variable, add 1 on the "onResume()" method and return to "0" when "onStop()" is called. now just evaluate when the "onResume()" is called and if the variable is "0" then is the first time, anything else do nothing.
I hope this helps cause there is no much information on your question to be more specific.

Categories

Resources