I m new to android world n m trying to make an app that wakes up my device from standy mode at a particular time.
I tried a few places where they mentioned about wakelock but this thing just prevents the device from going into standy mode.
I basically want my device to wake up from standby mode.
Like take the example of alarm clock... once set i can leave it and at that time from standby mode I get to see that the phone is active.
I even tried developer site for android... but it wasn't much helpful!
Any help would be highly appreciated!
You have to use Alarm Manager to schedule a repeated alarm (use RTC_WAKEUP for type parameter).
Then your code will execute even if the device is sleep. but notice that the registered alarm will removed once the device is restarted so you have a re-schedule the alarm again (by listening to boot broadcast)
Related
Basically I am trying to add an alarm feature to my app.
set time & date, save >> turn the app off >> when it's the time, RING-RING-!
But to do this, the app should wake itself to run the alarm feature; otherwise it won't work unless the app is running at the alarm time.
Any regular alarm apps just open itself when its the time, even when the app was not running on the phone. does anyone here know how apps self-wake?
Thanks very much
Use AlarmManager to do alarm envenif the app is not running.
But if the machine reboot, it will not work. So we need to run a service to register alarm when machine boot complete.
I program an application, which is connected to bluetooth device and used in a car (in the background) and navigation (or something else) in the foreground.
But on android 7+ (maybe also 6), application go to sleep after some time. When I'm trying to take picture from camera the "sleep mode" is immediately and my app is sleeping now (no bluetooth connection, no notifications) - just dead app.
I must go to recent apps -> click on my app and this make the "wake up". Bluetooth is now connecting to device again.
But I can't still check if the app is sleeping or not. So, how to keep app awake also in background?
I read some about WakeLock, but it looks like it's not working, cause app still sleeping. BUút maybe I'm using it wrong.
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wk = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "MyWakeLock");
wk.acquire();
and onDestroy
wk.release();
Thanks
From Android API 23 onward, Google has introduced Doze mode and App Standby in order to conserve battery power. When a device enters doze mode or an app enters App Standby, all tasks done by an app is deferred. We had an issue with an Alarm which was not firing because of the same. If you read upon docs, you will find that using a Wake Lock is not gonna help either.
Doze restrictions The following restrictions apply to your apps while
in Doze:
Network access is suspended.
The system ignores wake locks.
Standard AlarmManager alarms (including setExact() and setWindow()) are
deferred to the next maintenance window. If you need to set alarms
that fire while in Doze, use setAndAllowWhileIdle() or
setExactAndAllowWhileIdle(). Alarms set with setAlarmClock() continue
to fire normally — the system exits Doze shortly before those alarms
fire.
The system does not perform Wi-Fi scans.
The system does not allow sync adapters to run. The system does not allow JobScheduler to run
So if you want to bypass all these, your app must have a process in the foreground. Once again from the same doc -
The app has a process currently in the foreground (either as an
activity or foreground service, or in use by another activity or
foreground service).
In your case run a service and raise it's priority to foreground.
for wakelock to work, u should add this line to your androidmanifest file at the beginning. but as Ajay said, u should use a service so that the service works in background and do the job but if you want to keep the screen on that's something else which i think it is not the case here.
<uses-permission android:name="android.permission.WAKE_LOCK" />
xml layout file under layout type:
android:keepScreenOn="true">
why don't you use service? WakeLock basically keeps the screen on which is not an efficient mechanism
https://developer.android.com/training/scheduling/wakelock.html
How can I know in my app if the phone is in Doze mode or if my app is in Standby mode?
Can I also know it by using some subscribing approach?
There's a difference between App Standby (where your app goes to
sleep because it's lonely) and Doze (where the system went to
sleep because users have lives).
Doze only works when your device remains stationary. If you pick it up or wake the screen, Doze mode is deactivated and the timer
resets. It won’t ever turn on when the phone is sitting in your pocket
unless you’re sitting remarkably still. Doze simply keeps your phone
from burning through the battery while it’s asleep on a desk or table
or something.
An app that goes into Standby loses all network access and all its
background sync jobs are suspended. These restrictions are temporarily
lifted when your phone is plugged in and for a few minutes every day
or two. This gives suspended apps a chance to run any pending sync
jobs, but they won’t be allowed to continue running. A high-priority
push notification will also be able to wake an app from Standby for a
short time.
Note: Google has also noted that it isn’t acceptable for apps to
fire off notifications simply to remain exempt from App Standby. This
sort of behavior can get an app banned from the Play Store.
For more details with testing Doze and App Standby, Please visit this link: https://developer.android.com/training/monitoring-device-state/doze-standby.html
Thanks!
You don't need to do anything to manage Standby mode. You just need to manage you lifecycle as usual. There will be a time in the day when you app is gonna be resumed (so you can sync data and resume delayed jobs), but notifications and alarms will always bring your app out of standby.
Check this out: https://plus.google.com/+AndroidDevelopers/posts/76eh5MaJaXj?linkId=18331776
I am writing a Android client app which keeps receiving push notifications from a server using HTTP long-polling(Comet).
I don't want to always keep WakeLock since it will drain battery, but I need to make sure the device can receive notification even when it is in sleep mode.
And I found this question:
Android: Sleep stages/levels on an Android device?
where "CommonsWare" mentioned that an incoming packet on a non-Wifi socket will wake up the device.
So my solution looks like this:
Client ------------------------- Server
---- Request----->
release WakeLock (Allow device to sleep)
<----Notification-- (Hopes it can wake up the device)
require WakeLock
process the notification
---- Request----->
release WakeLock
....
But there is a little time window between receiving the notification and requiring the wakelock, so my question is, how long will the device keep this awake state? Is it possible for the device to back to sleep during this time window?
The device will be awake for long enough to execute some short code in the BroadcastReceiver. I have not been able to find an exact number of millis, but the idea is that in your receiver, you should grab whatever WakeLock you need to proceed with your own processing.
However exact management of the WakeLock can be tricky. I recommend using #CommonsWare's WakefulIntentService:
https://github.com/commonsguy/cwac-wakeful
I wrote a little widget for Android devices.
The widget uses AlarmManager to set recurring updates.
I'm using the RTC clock for the AlarmManager.
According to the documentation, if the device is sleeping, the RTC clock won't wake up the device and the next update will be when the device is woken.
I have a log file for the widget which shows when it was updated.
Even when I don't touch the device , I still see updates in the log file.
Why is that? shouldn't the device be sleeping and thus my widget shouldn't update?
I'm using System.currentTimeMillis() for the starting time so it should be correct for RTC clock.
You need to disconnect device from your desktop. When device is charging it doesn't go to sleep mode even when screen is black.