I want to disable the lock screen in a certain app. My problem is that this also disables the lock screen for the whole phone. So I added lock.reenableKeyguard(); to the onPause() method, but this is called not only when the user exits the app but everytime a new activity starts in that app. Do I have to disable the lock screen in every activity and add this onPause method? Is there way to manage this only in the first activity that is shows when the app starts?
Btw I also reenable the lock screen in the onStop and onDestroy methods. The other problem with onPause is that it is also called when I turn off the phone's screen (by pressing the power button or what is that called), so when I first open the app, I turn off the screen then turn it on, there is no lock screen, but when I turn it off-on again, there is it, as I reenable it in the onPause method.
After playing with this a couple of hours I figured out I forget to implement onResume():
public void onResume() {
lock.disableKeyguard();
super.onResume();
}
This way every time I open the activity/turn on the screen, the lock screen is disabled.
Related
I want to clear shared preference when I press home button then the app is cleared swipe or simply cleared from device ram. What callback should I use ? (Like OnDestroy, but OnDestroy is not working for me)
Use onPause() for doing stuff when the activity loses focus (multi-window mode, home button pressed, swiped away, or anything that takes focus away from the activity). The rest aren't guaranteed to be called.
Some apps in Android seem to be able to keep the screen on beyond the normal timeout (presumably done with FLAG_KEEP_SCREEN_ON), but on exiting the app lock the screen. I've seen this e.g. in navigation apps, that keep route guidance open, but the screen locks as soon as you leave the app.
How is this done?
(Note that the lock should happen when the application is goes into the background, not just when an activity is replaced by another activity within the same application.)
I managed to partially figure this out, based on "Launch activity when user taps on a notification from the lockscreen". Here is an example. Use this project https://github.com/googlesamples/android-CustomNotifications/ (e.g. Android Studio: File > New > Import Sample; look for ("Custom Notifications") and replace onCreate like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.sample_main);
}
and in the manifest add
<activity ...
android:showOnLockScreen="true"
...
You now observe that the activity persists if
You press power button to turn off the screen. Press power button to activate screen, and your activity resumes without need to unlock screen.
You let the screen time out: Again, press power button to activate screen, and your activity resumed without need to unlock screen.
But: Once the screen has been locked at least once (through timeout or power buttons), then, when you navigate away from the app, the screen locks (which provides a partial answer to Lock screen programatically when exiting app).
However, it doesn't work when you navigate away before the screen has been locked at least once. Does anybody have suggestions?
Related question here: Launch activity when user taps on a notification from the lockscreen (secure unlock)
I've got a fragment where I'm recording audio, I want to pause the recording when a user presses the home key, but continue to record on a rotation.
In onPause, onStop or some other method that gets called, how can I differentiate between when the user has pressed home, or whether the fragment is being closed down because of a rotate?
I've looked into listening for the home key press, but everything I find says that isn't supported/won't work/can't do it.
I've tried using an OrientationEventListener, but it seems to get called nonstop, and if I'm looking for 0, 90, 180, and 270, my code still gets run if orientation goes from 1 to 0.
There has to be some way to differentiate that!? Any ideas?
EDIT is this my best option? -->
http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange
EDIT
When I press the home button, I need to stop recording in onPause or onStop. When I rotate the device, I need to continue recording through those methods. So I need something I can check in those methods or before that tells me whether this is a home press or a rotation.
SOLUTION
I ended up just locking the orientation of the fragment when it's started and not allowing rotation. Not ideal, but more ideal than any other solution.
I have an activity that starts the Vibrator system service in its onCreate method, then when the user pushes a button it cancels the vibrator, then calls finish() to close the activity.
This activity is brought up via the AlarmManager, so when it gets closed it will return the user to whatever app they currently had open (not necessarily mine).
The problem I'm having is if my activity is in landscape mode, and the user is brought to a screen that doesn't support landscape (such as the home screen) when the activity closes, my application switches to portrait and calls onCreate() before actually closing my screen. So the steps causing this problem are as follows...
The activity is lauched in portrait mode
onCreate method gets called, which starts the vibrator
The user rotates the phone to landscape mode
onCreate is called again, but because onSaveInstanceState is not null, I can skip starting the vibrator again
The user pushes the button to close the screen
I call vibrator.cancel()
I call finish()
Because the screen the user will be brought back to a screen that doesn't support landscape mode, my activity calls onCreate()
savedInstanceState equals null, so the vibrator gets started again
My app is closed with the vibrator still running
Currently the only way I can think of to rectify this is to make my activity only support portrait mode, but I'd like to avoid that if I can. Does anyone know a way in which I can prevent onCreate() from being called after I call finish()?
why dont you call method
onPause() or onStop() and inside this methods call vibrator.cancel()
onStop() always gets called when your app is not visible anymore.
check out this flowchart
I want to display deals from website when the screen is locked in an android application.
I succeeded to display the activity when the screen is locked. But while pressing the home key of the device the activity exits. So I want to know exactly how we detect the home button on the device is pressed or not.
You can save the state of your Activity on the SharedPreferences so in your activities onStop method you can set the variable "running" to false and then on your onResume method set it to true.