Wake up Android Wear from a service when in sleep mode? - android

I am working on Android Wear app that is somehow a timer that displays a notification when the time is elapsed and then start again. The notification displays a message on the watch and make the device vibrating.
The app is working fine except when the watch is in sleep mode. In this case, the user needs to touch the screen in order that the watch vibrates and the timer starts again.
The notification is created and displayed from a WearableListenerService. I tried many solutions such as Wake locks. The code is written in C# with Xamarin.Android but i think any java dev will also be able to read:
static PowerManager.WakeLock sWakeLock;
static object LOCK = new object();
void DisplayNotification()
{
//create the wake lock
lock (LOCK)
{
if (sWakeLock == null)
{
// This is called from BroadcastReceiver, there is no init.
var pm = (PowerManager)GetSystemService(Context.PowerService));
sWakeLock = pm.NewWakeLock(
WakeLockFlags.ScreenBright | WakeLockFlags.Full | WakeLockFlags.AcquireCausesWakeup, "My WakeLock Tag");
}
}
sWakeLock.Acquire();
//display the notification
//....
//release the wake lock
lock (LOCK)
{
//Sanity check for null as this is a public method
if (sWakeLock != null)
sWakeLock.Release();
}
}
My issue is that the watch will wake up at some point (after few minutes) but not immediately when requested. I tried to put a delay on the release of the wake lock or to make an sWakeLock.Acquire(2000) in order that the wake lock is automatically released after 2 secs. Then, I also tried other solutions with the WakefulBroadcastReceiver and the AlarmManager but I still have the same behaviour.

I've done something like that on mobile just by adding one line in onCreate of the activity i was calling by service.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//add this below getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}

I would avoid Xamarin for Wear as it might cause some conflicts and not work well as it wasn't designed to.
Also if you created Service that is triggered on Android Wear - I would say Message API is correct way to do it, you should have no problem with a creating notification (on a watch itself) with vibrations (without need to touch screen) and update notification from your Service - to update timer value.
No wake locks needed.
Might help: How can I create notification that is different on device and wear?

Related

How can I make my app running also in Doze mode without Partial_Wakelock? [duplicate]

One of my peer developer has written an intent service that makes an API call and then sleeps for 2 mins. After waking up, it sends again.
Below is the code:
public class GpsTrackingService extends IntentService {
....
#Override
protected void onHandleIntent(Intent intent) {
do{
try{
//make API call here
//then go to sleep for 2 mins
TimeUnit.SECONDS.sleep(120);
} catch(InterruptedException ex){
ex.printStackTrace();
}
} while (preferences.shouldSendGps()); //till the user can send gps.
}
....
}
Manifest
<service android:name=".commons.GpsTrackingService" />
This is working fine when the phone is active. However, whenever the phone goes into doze mode it fails to wake.
Will using alarm manager with WAKE permission solve this?
I have just got the code base and need to fix this within today. It'll be great if someone can help.
As the documentation says:
In Doze mode, the system attempts to conserve battery by restricting
apps' access to network and CPU-intensive services. It also prevents
apps from accessing the network and defers their jobs, syncs, and
standard alarms.
Periodically, the system exits Doze for a brief time to let apps
complete their deferred activities. During this maintenance window,
the system runs all pending syncs, jobs, and alarms, and lets apps
access the network.
In few words, while in Doze mode the system suspends network accesses, ignores Wake Locks, stops acquiring data from sensors, defers AlarmManager jobs to the next Doze maintenance window (which are progressively less frequently called), also WiFi scans, JobScheduler jobs and Sync adapters do not run.
Neither setAndAllowWhileIdle() nor setExactAndAllowWhileIdle() can fire alarms more than once per 9 (?) minutes, per app.
And it seems that the Foreground Services are also involved into this "Doze Drama", at least in MarshMellow (M).
To survive in this situation, tons of applications need to be at least rewiewed. Can you imagine a simple mp3 player which stops playing music when the device enters in Doze Mode?
Doze mode starts automatically, when the device is unplugged from the power supply and left on the table for about 1 hour or so, or even earlier when the user clicks the power button to power down the screen, but I think this could depend by the device manufacturer too.
I tried a lot of countermeasures, some of them really hilarious.
At the end of my tests I reached a possible solution:
One possible (and maybe the only) way to have your app running even when the host device is in Doze mode, is basically to have a ForegroundService (even a fake one, doing no jobs at all) running in another process with an acquired partial WakeLock.
What you need to do is basically the following (you could create a simple project to test it):
1 - In your new project, create a new class which extends Application (myApp), or use the
main activity of the new project.
2 - In myApp onCreate() start a Service (myAntiDozeService)
3 - In myAntiDozeService onStartCommand(), create the Notification
needed to start the service as a foreground service, start the
service with startForeground(id, notification) and acquire the
partial WakeLock.
REMEMBER! This will work, but it is just a starting point, because you have to be careful with the "Side Effects" this approach will generate:
1 - Battery drain: The CPU will work for your app forever if you
don't use some strategy and leave the WakeLock always active.
2 - One notification will be always shown, even in the lockscreen,
and this notification cannot be removed by simply swiping it out, it
will be always there until you'll stop the foreground service.
OK, let's do it.
myApp.java
public class myApp extends Application {
private static final String STARTFOREGROUND_ACTION = "STARTFOREGROUND_ACTION";
private static final String STOPFOREGROUND_ACTION = "STOPFOREGROUND_ACTION";
#Override
public void onCreate() {
super.onCreate();
// start foreground service
startForeService();
}
private void stopForeService() {
Intent service = new Intent(this, myAntiDozeService.class);
service.setAction(STOPFOREGROUND_ACTION);
stopService(service);
}
private void startForeService(){
Intent service = new Intent(this, myAntiDozeService.class);
service.setAction(STARTFOREGROUND_ACTION);
startService(service);
}
#Override
public void onTerminate() {
stopForeService();
super.onTerminate();
}
}
myAntiDozeService.java
public class myAntiDozeService extends Service {
private static final String TAG = myAntiDozeService.class.getName();
private static boolean is_service_running = false;
private Context mContext;
private PowerManager.WakeLock mWakeLock;
private static final int NOTIFICATION_ID = 12345678;
private static final String STARTFOREGROUND_ACTION = "STARTFOREGROUND_ACTION";
private static final String STOPFOREGROUND_ACTION = "STOPFOREGROUND_ACTION";
#Override
public void onCreate() {
super.onCreate();
mContext = getApplicationContext();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!is_service_running && STARTFOREGROUND_ACTION.equals(intent.getAction())) {
Log.i(TAG, "Received Start Foreground Intent ");
showNotification();
is_service_running = true;
acquireWakeLock();
} else if (is_service_running && STOPFOREGROUND_ACTION.equals(intent.getAction())) {
Log.i(TAG, "Received Stop Foreground Intent");
is_service_running = false;
stopForeground(true);
stopSelf();
}
return START_STICKY;
}
#Override
public void onDestroy() {
releaseWakeLock();
super.onDestroy();
}
private void showNotification(){
Intent notificationIntent = new Intent(mContext, ActivityMain.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(mContext)
.setContentTitle("myApp")
.setTicker("myApp")
.setContentText("Application is running")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent)
.build();
// starts this service as foreground
startForeground(NOTIFICATION_ID, notification);
}
public void acquireWakeLock() {
final PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
releaseWakeLock();
//Acquire new wake lock
mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG+"PARTIAL_WAKE_LOCK");
mWakeLock.acquire();
}
public void releaseWakeLock() {
if (mWakeLock != null && mWakeLock.isHeld()) {
mWakeLock.release();
mWakeLock = null;
}
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
AndroidManifest.xml changes.
In the AndroidManifest.xml add this permission:
<uses-permission android:name="android.permission.WAKE_LOCK" />
Don't forget to add the name of your app in the <application> tag:
<application
....
android:name=".myApp"
....
And finally add your foreground service running into another process:
<service
android:name=".myAntiDozeService"
android:process=":MyAntiDozeProcessName">
</service>
A couple of notes.
In the previous example, the notification created, when clicked,
opens the ActivityMain activity of your test project.
Intent notificationIntent = new Intent(mContext, ActivityMain.class);
but you can use another kind of intent too.
To test it, you have to add some job to be performed into your
ActivityMain.java, for example some repeating alarm (which was
normally stopped when the device falls in Doze Mode), or a ripetitive
network access, or a timed tone played, or.... whatever you want.
Remember that the job performed by the main activity has to run
forever because to test this AntiDoze you need to wait at least 1
hour to be sure the device enters in Doze Mode.
To enter in Doze mode, the device has to be quiet and unplugged, so
you can't test it while you are debugging. Debug your app first,
check that everything is running then stop it, unplug, restart the
app again and leave the device alone and quiet on your desk.
The adb commands suggested by the documentation to simulate Doze
and StandBy modes could and could not give you the right results
(it depends, I suppose, by the device manufacturer, drivers, bla
bla). Please make your tests in the REAL behaviour.
In my first test, I used an AlarmManager and a tone generator to play a tone every 10 minutes just to understand that my app was still active.
And it is still running from about 18 hours, breaking my ears with a loud tone exactly every 10 minutes. :-)
Happy coding!
One of my peer developer has written an intent service that makes an API call and then sleeps for 2 mins. After waking up, it sends again.
Only have a service running while it is actively delivering value to the user. Sitting around for two minutes, watching the clock tick, is not actively delivering value to the user.
Will using alarm manager with WAKE permission solve this?
That depends on what you mean by "solve this". You can use AlarmManager to request to get control every two minutes so that you can do work. While the device is in Doze mode, you will not actually get control every two minutes, but once per maintenance window.

Prevent app from going on OnPause when turning screen off using partial wakelock

I'm trying to catch the volume Up/Down button press with the screen off, but despite acquiring a wakelock the app doesn't stay in the foreground when pressing the lock button. I have a breakpoint in the OnPause method and it gets hit when the screen turns off, and I can confirm that the wakelock is on through ADB terminal with the command:
adb shell dumpsys power
I omitted the volume buttons events since they're not relevant, but they do work when the screen is on.
I don't know what I'm missing, maybe I'm missunderstanding how the wakelock is supposed to behave?
I'm testing it in an emulator with Android 6.0 and in a physical phone with Android 7.1.2.
Thanks in advance for any help..
Button getLockBtn;
PowerManager _powerManager;
PowerManager.WakeLock _wakeLock;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
RequestWindowFeature(WindowFeatures.NoTitle);
SetContentView(Resource.Layout.Main);
getLockBtn = FindViewById<Button>(Resource.Id.GetLockBtn);
_powerManager = (PowerManager)GetSystemService(PowerService);
_wakeLock = _powerManager.NewWakeLock(WakeLockFlags.Partial, "MyTag");
getLockBtn.Click += (s, e) =>
{
if (_wakeLock.IsHeld)
{
_wakeLock.Release();
getLockBtn.Text = "Get lock";
//Remove the notification
}
else
{
//Here I show a notification
_wakeLock.Acquire();
getLockBtn.Text = "Release lock";
}
};
}
So how do apps like Google Maps achieve that behavior
When the screen is locked, the volume control comes from the system, and it is not the time to apply the control itself.Google Maps do nothing with the volume.
Your problem may not be the use of the volume button under the lock screen, but the program can still work when you lock the screen.
If you want a task to continue running, even when your Activity is not active, you need to use a Service. https://developer.android.com/guide/components/services.html.
Also can and a Notification to show something on the lock screen. https://developer.android.com/guide/topics/ui/notifiers/notifications.html

Application not working on locked screen

I'm creating an application that needs to keep track of a lot of things the user is doing. This all needs to happen in the background and needs to be running continiously. To mention two: constant tracking of activity with google activity API and location tracking with google's geofence API.
I have created a (Intent)Service for each parameter I am tracking and everything is going well. Even when you kill the application, the service will start again on the background and perform as expected.
MY PROBLEM: As soon as I lock my phone, it stops tracking. As soon as I light up my screen or unlock it works again.
NOTE1: I'm using one main service which controls all other (intent)services sensing the parameters. (I'm not sure if this is the best way to do it or if I should create one big service?...).
What I've tried is the PARTIAL_WAKE_LOCK to keep awake my main service controlling all other services. This didn't work. I've also tried to use the PARTIAL_WAKE_LOCK to keep awake the services sensing the parameters. This did not work either.
NOTE2: I know this should not be done in real applications but it's a must. It's an application for an experiment and will never go in the playstore. Also all users will be notified what will be tracked and how this can effect their battery during the experiment.
#Override
public void onCreate() {
super.onCreate();
...
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakelockTag");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (! keepCPUOn){
wakeLock.acquire();
keepCPUOn = true;
}
}
Manifest:
<uses-permission android:name="android.permission.WAKE_LOCK" />
Many thanks!
Beginning with Android 6 and its new Doze mode the wake locks don't prevent the device to enter sleep mode, they are ignored
Proposed solution: Your service need to be a foreground service, that's it should call startForeground() and show a non dismissable notification, and you need to acquire a partial wake lock too.
I am also creating app like your. location tracking work in background and UI screen doesn't lock. in which UI screen you are start location service please put below code in that java file.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_current_ride_new);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakelockTag");
wakeLock.acquire();
}
This code is working perfectly for me.
I've just add my app in Protected apps in settings my phone Android 4.4 (this flug allow to keep running after the screen is turned off)

request location updates not working during device sleep

I am using location manger class to receive location updates my requirement is such that I have to listen for contious location updates but the problem I am facing that once it disconnects I don't know how to reatablish GPS connection,furthermore in some device once device sleeps i m not able to receive any location updates please provide any solutions to achieve this any help is appreciated..
public void setup(MainActivity activity) {
if (!setup) {
this.activity = activity;
Intent locationIntent = new Intent(this.activity, LocationIntentService.class);
mLocationPendingIntent = PendingIntent.getService(this.activity, 0, locationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent detectedIntent = new Intent(this.activity, ActivityDetectionIntentService.class);
mDetectedActivityPendingIntent = PendingIntent.getService(this.activity, 0, detectedIntent, PendingIntent.FLAG_UPDATE_CURRENT);
googleApiClient = new GoogleApiClient.Builder(activity)
.addConnectionCallbacks(activity)
.addOnConnectionFailedListener(activity)
.addApi(ActivityRecognition.API)
.build();
setup = true;
}
}
**LocationIntentService.java**
public class LocationIntentService extends IntentService {
public LocationIntentService() {
super("LocationServices");
}
public LocationIntentService(String name) {
super("LocationServices");
}
#Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
Location location = intent.getParcelableExtra(LocationManager.KEY_LOCATION_CHANGED);
if (location != null) {
Intent localIntent = new Intent(HWUtil.LOCAL_RECEIVER);
localIntent.addCategory(Intent.CATEGORY_DEFAULT);
localIntent.putExtra(HWUtil.LOCATION, location);
localIntent.putExtra(HWUtil.FROM, HWUtil.LOCATION_SERVICE);
sendBroadcast(localIntent);
}
}
}
}
and i m sending this location updates to Broadcastrecciver
Please note that the continuous usage of pure GPS as location provider is quite energy hungry on mobile devices. Once that is said, I would perform your task as follows:
I would use a (background) service that would be working togheter with your mobile app. I.e., the mobile app will start the execution of this service (check startForeground() functionality so that your service could be run almost with no interruption, and including a notification on statusBar that can be linked to your activity).
The service (or any internal class) would implement the LocationListener interface and will be the one that actually will be sampling locations. Once you get a location you will process it (depending on your requirements you might want to process it in another thread since the default thread of a created service is the same than the Activity UI).
Once processed, youw would deliver the response to the Android activity, i.e., you would call a public method of your activity, or would implement a more complex communication strategy with Handlers, etc.
In regard with the continuous location sampling, I would strongly suggest you to use AlarmManager services so that you could schedule the next readings (you could make it at exact repeating intervals, see official developer's documentation).
If (and only if) the processing of the location update is heavy or time consuming (for instance, you have to transmit it to a server) you could acquire and hold a WakeLock for avoiding the device to fall into sleep mode; do not forget to release the WakeLock after your processing is done, since it is a major cause of energy sinks in mobile apps, so be very careful with this.
Hope it helps for you
AFAIK, wake lock is the easiest way to do. PowerManager.WakeLock
wake lock is a mechanism to indicate that your application needs to have the device stay on.
Any application using a WakeLock must request the android.permission.WAKE_LOCK permission in an element of the application's manifest. Obtain a wake lock by calling newWakeLock(int, String).
Call acquire() to acquire the wake lock and force the device to stay on at the level that was requested when the wake lock was created.
You should aquire a wake lock:
//in onCreate of your service
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
cpuWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"gps_service");
cpuWakeLock.acquire();
// Release in onDestroy of your service
if (cpuWakeLock.isHeld())
cpuWakeLock.release();
and add this to your manifest:
https://developer.android.com/reference/android/Manifest.permission.html
Above is good if you need continuous location updates.
I tried to add this code to my project in my MainActivity/onCreate void:
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyApp::MyWakelockTag");
wakeLock.acquire();
Then I tested it my tablet. I was working over ~25 mins but then it stopped again. Sending of the location data was stopped. Then I pushed the pwr button of the tablet and the GPS ikon disapeared on the top of the screen and did not come back...

Timer stops every time the phone sleeps

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
}
}

Categories

Resources