I have Created a methods to send SMS and call under function callfunction();
I want to call that function when the user have pressed four time power button, It should not required that app should be in launched state.
Try using the Power Manager
PARTIAL_WAKE_LOCK
added in API level 1
int PARTIAL_WAKE_LOCK
Wake lock level: Ensures that the CPU is running; the screen and keyboard backlight will be allowed to go off.
If the user presses the power button, then the screen will be turned off but the CPU will be kept on until all partial wake locks have been released.
Constant Value: 1 (0x00000001)
Example:
//Initialize the Power Manager
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
//Create a PARTIAL_WAKE_LOCK
//This will keep the cpu running in the Background, so that the function will be called on the desired Time
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
//Check if the WackLock is held (may throw erro if you try to acquire twice)
//TRUE --> Do nothing, all good
//FALSE --> Acquire the WakeLock
if(!wl.isHeld()){
wl.acquire();
}
//*****
//You code to handel the Powerbutton comes here
//*****
//If the Repeating task is not active, release the Lock
//Check if the WackLock is held (may throw error if you try to release a none acquired Lock)
//TRUE --> Release Lock
//FALSE --> Do nothing, all good
if(wl.isHeld()){
wl.release();
}
To handle the Power Button, check this Post:
How to hook into the Power button in Android?
This is just an assumption, if it still doesnt work, could you post some Logs or more code snippets of your project :)
I think this is not possible to keep track of power button press for 4 times and to start your expected functionality. Since i don't find any app on play store which has this kind of functionality so as to start there respected jobs.
But you will get trigger for power button press for 1 time.
Related
I'm creating this incredibly simple app, since I'm just starting with Android and I have no experience with Java what so ever. Anyhow, all I have is a timer and a button. Obviously the button starts the timer which counts down from 60 ( 1 minute ) and then vibrates when it's done. All worked fine up until the point I decided to press the lock screen button to put the phone to sleep. I found out that the timer in my app stops going until I unlock the phone. This also happens if I set the auto sleep time to less than 60 seconds and the phone falls asleep on it's own. My question is - how can I have that chronometer running even when the screen is not active?
You need to make this using BroadCastRecievers for system-calls. (Related Helpful question)
You can also play off the life-cyles of the Activity using the PowerManager.
Example using PowerManager:
#Override
protected void onPause()
{
super.onPause();
// If the screen is off then the device has been locked
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
boolean isScreenOn = powerManager.isScreenOn();
if (!isScreenOn) {
// The screen has been locked
// Continue your timer
}
}
I am coding an application which will trigger some action (like toast message) every 5 minute after the Screen is ON. I have made a 'Service' for this purpose. My Service is running successfully and triggering the specified action every 5 minutes.
My 'Service' keeps on running even when the Screen is OFF. I don't want to perform any action when the Screen is OFF. Should I stop the service on every screen OFF and re-run it on the next screen ON? or should I keep it running even when the Screen is OFF?
I just want to decrease the load on CPU performance because of the background running 'Service'.
Let the service keep running, and display the Toast only if the screen is off using following code,
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
Now,
if ( isScreenOn )
{
// Display Toast
}
else
{
// Do nothing
}
I use wakelock to keep the display on.
However, when the app is ended, the display also be turned off.
I want to keep it on after the app is ended.
What should I do?
What you want to do is use the .On_AFTER_RELEASE flag see below.
PowerManager pm;
PowerManager.WakeLock wakeLock;
// Put the next 5 lines in a method that is called when you want to acquire a wakelock //or in the OnREceive() of a broadcast receiver.`
PowerManager pm = (PowerManager)
context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(
pm.ON_AFTER_RELEASE|pm.SCREEN_DIM_WAKE_LOCK, "My wakelook");
wakeLock.acquire();
...Do Stuff here
//Override the onDestroy of your activity to release the wakelock when it is destroyed.
#Override
protected void onDestroy() {
super.onDestroy();
wakelock.release;
}
This will keep the screen lit up as if someone interacted with the device (pushed the power button, touched the screen, etc.) right when you release the wakelock. At that point a normal power down will occur if there is no further interaction with the device (screen goes dim, and then it goes dark).
I
Let me know if that helped.
Thx
******************Added*************
If you want to wake up the phone from after the screen goes dark you can create your wakelock with the following flags. This is good if you have a broadcast receiver and want to turn on the screen when an event happens so the user can immediately see a UI (Notification, Toast, Layout,etc.)
PowerManager.WakeLock wakeLock = pm.newWakeLock(
pm.ACQUIRE_CAUSES_WAKEUP|pm.ON_AFTER_RELEASE|pm.SCREEN_DIM_WAKE_LOCK, "My wakelook");
And for how long do you need the display to stay on? Just for some time, or until certain event happen?
Don't release the wakelock when application is ended. You may schedule an event for a period of time you need to keep the screen on. But generally it's a bad practice as holding full wakelock may prevent not only screen but other hardware from going to sleep.
I want to take pictures from the Android device's camera periodically over a matter of hours, to create a time lapse video effect.
I set an Alarm Manager with an AlarmManager.RTC_WAKEUP flag set to start up a service every few minutes.
The service holds a partial wakelock, does some work, and then calls a Broadcast Receiver through the Alarm Manager which starts up an Activity.
The activity is created (or is resumed), turns on it's own wakelock, and sets up the camera preview surface. Once the surface is setup the SurfaceHolder listener's surfaceChanged() method is called, which finally takes a picture.
If the device is awake, everything works perfectly as expected. But if the device is asleep, once the Activity's onResume() method is finished the Activity is instantly paused. The camera's preview surface never finishes initializing, and no picture will ever be taken.
So the questions I have are:
Is there any way to wake up the phone programmatically? I even try using:
PowerManager powerManager =
(PowerManager)this.getSystemService(Context.POWER_SERVICE);
powerManager.userActivity(SystemClock.currentThreadTimeMillis(),false);
But that doesn't wake up the phone if it is asleep.
Is there any way to take a picture without using a preview surface view?
Is there a way to take a picture that doesn't rely on asynchronous callbacks? Can I put all the code in the Activities onResume() method to take a picture?
Is there any way to keep the Activity's onResume() method running long enough so that the camera's preview has enough time to initialize and call all the listeners?
I am using the wakelocks correctly, and I have all the permission's set properly in the manifest file. My activity isn't kept awake long enough for the asynchronous listeners to properly work.
And to compound the issue, I'm trying to keep everything Android 1.6 compatible, because that is the only test device I have access to.
This is frustrating stuff!
I have finally gotten somewhere now.
I have to create a wakelock using these two flags
PowerManager.SCREEN_BRIGHT_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP, "bbbb");
wl.acquire();
Then the device wakes up, and starts at the keyguard screen.
But the only way I can get past the keyguard screen and take a picture is to use these flags on the Activity's window:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
But this is only available with Android 2.0, and doesn't work in 1.6.
You can also disable the Keyguard screen with
KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
km.newKeyguardLock(TAG).disableKeyguard();
provided you have the DISABLE_KEYGUARD permission.
That's available since API Level 1.
Are you doing something like this in your onResume method
.... onResume() {
....
WakeLock myWakeLock = .....;
...
}
If so, as soon as the method exits, the WakeLock is released, and the device is free to do whatever it feels like doing ( which is likely to go back to sleep )
and you will need to store the WakeLock in the class somewhere, not as a function local.
I'm using Wakelock in my application to prevent the phone from sleeping when the app is visible.
The problem comes when there is an orientation change and the system destroys and re-creates the app in landscape mode. If the wakelock timer has expired in the background the system takes the release of wakelock as an opportunity to switch off the screen.
Edit: I should mention that I am setting Wakelock onResume, and releasing onPause - as I understand it, it's necessary to release then to prevent the app from leaking the wakelock.
I need wakelock to continue through the orientation change.
Below I've outlined a solution to this. Is this the preferred way to handle it, and are there any alternatives?
Create a service which holds wakelock (rather than the activity) and when the activity unbinds the service starts a countdown timer (for say 10 seconds) in which it will release wakelock when the timer expires if the activity does not rebind. If it was a simple orientation change the activity will rebind within that 10 seconds and so maintain wakelock, if it doesn't, wakelock will be released.
Thanks.
Instead of a WakeLock, try getWindow().setFlags() using the WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON flag.
If the wakelock timer has expired in
the background the system takes the
release of wakelock as an opportunity
to switch off the screen.
It shouldn't. By definition, the user has interacted with the device, so the screen should stay on for that reason, independent of anything else.
Here's how I did it...
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK|PowerManager.ON_AFTER_RELEASE, "DoNotDimScreen");
Followed by...
wl.acquire();
... when you need to actually activate the wake lock.
The 'PowerManager.ON_AFTER_RELEASE' flag pokes the user activity timer so the screen stays on for a little longer when this wake lock is released, which will be the case when your activity is Destroyed on a change of orientation.
Works OK for me!
I did it by actually never releasing the wakelock. Instead, I acquire it with timeout.
private PowerManager.WakeLock wl;
protected void onStart(Bundle savedInstanceState) {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "Words counter");
// disable reference counting, as acquire/release calls are not paired
wl.setReferenceCounted(false);
wl.acquire();
}
protected void onStop() {
// acquire for few seconds to keep lock until onStart is called with new orietnation
wl.acquire(3000);
}
Advantages over other methods:
Usable with the SCREEN_DIM_WAKE_LOCK
Simple (no need to play with isFinishing() etc.)
Does not poke user activity after orientation change. I.e. dimmed display stays dimmed
If you release (e.g. the time is over but the user remained in the activity), it's released immediately. No need to wait for default timeout.