Android Wear - Wake lock not waking up watch - android

I am trying to make the watch vibrate at specific moments that are not at regular intervals.
I have no problem doing it when the watch's screen is on but it is not working when the watch screen dims out.
I have read a few questions on here and android developer's pages on WakeLocks and I think it's what I need... However it does not work for me. Below is my code, am I doing something wrong?
First, in my Manifest file, I have:
<uses-permission android:name="android.permission.WAKE_LOCK" />
Here is parts of my code:
PowerManager.WakeLock wakeLock;
#Override
public void onCreate(SurfaceHolder holder) {
super.onCreate(holder);
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, TAG);
}
private void vibrate(int duration) {
wakeLock.acquire();
Vibrator v = (Vibrator) getBaseContext().getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(duration);
wakeLock.release();
}
The screen doesn't turn on and the watch doesn't vibrate... What am I doing wrong?
Thank you!

If your watch doesn't vibrate when you get a notification or you're not seeing notifications at all, check your watch after each step to see if notifications start working. try the troubleshooting steps below:
Make sure that your watch isn't in Cinema mode.
Make sure that you're getting notifications on your phone.
Make sure that you haven't turned off (block) notifications for specific apps.
Make sure that app notifications and system notifications on your phone are turned on.
Check that your phone is connected to the Internet.
Make sure that your watch is paired with your phone or tablet.
Try restating your phone and your watch.
Check that these apps are up to date: Google Play Services, Google, Android Wear.
Try to awake device by using the flag FLAG_KEEP_SCREEN_ON.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
The advantage of this approach is that unlike wake locks, it doesn't require special permission, and the platform correctly manages the user moving between applications, without your app needing to worry about releasing unused resources.
Another way to implement this is in your application's layout XML file, by using the android:keepScreenOn attribute:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">
...
</RelativeLayout>
Using android:keepScreenOn="true" is equivalent to using FLAG_KEEP_SCREEN_ON. You can use whichever approach is best for your app.

You need to keep the wake lock acquired for as long as you want the watch to stay interactive (and vibration to continue -- you must be in interactive to vibrate), but you seem to be releasing the wake lock immediately after acquiring it.
Acquiring wake locks can be risky, so if you're running that code from a watch face service (not clear from your snippet), start a transparent activity when acquiring your wake lock and release it when the activity dies -- this way, you are using the activity lifecycle as a means to stop the vibration and release a wake lock. This is something I implemented in the ustwo Timer Watch Faces (the watch face starts flashing and vibrating in interactive mode when the timer expires), and it's worked well.
Also, remember that to make vibration work, you need to declare the vibrate permission in the manifest:
<uses-permission android:name="android.permission.VIBRATE" />

Related

Is it possible for my GPS application to continue running in the background while the tablet is on standby?

I have created an application that generates a tracklog of the Android devices location. A GPS coordinate is recorded at regular intervals and stored on the device for later download. Currently, when the phone goes on standby, the program stops recording points. Is there a method that would allow the application to continue documenting location while the unit is on standby? Thanks in advance.
According to android documentation, if your app targets API level 26 or higher, the system imposes restrictions on running background services when the app itself isn't in the foreground. Also for accessing location in the background you may need additional permissions
You can run a foreground service with showing an ongoing notification if you want to run a service which is always alive in the background. Or you can schedule tasks using WorkManager.
I found two solutions.
1.) Use Wakelock
public void wakeLock() {
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "MyApp::MyWakelockTag");
wakeLock.acquire();
}
with the following added to the manifest XML file,
<uses-permission android:name="android.permission.WAKE_LOCK"/>
or, 2.) use WindowManager to keep the device awake,
public void noSleep() {
if (bNoSleep == true){
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} else if (bNoSleep != true){
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
I've chosen the latter as a user selectable feature accessible via checkbox in the main workspace. I've also set this to engage automatically when a tracklog is initiated with the user having the option to disable this and allow standby/sleep to occur. I did implement a wakelock, but had some issues with it that may be related to a custom ROM on some of my Android devices. This is why I went ultimately went w/the windowmanager solution.

How to connect Android Application to Internet when the screen is locked?

After some research on Google Search, I tried to find a solution to connect my Android Application to internet when the screen is locked and without the phone charging.
My first impression is that the phone block in background my application. Strange things because others applications on Google Play Store can communicate on the Internet and do things on background.
I have already tried to search solutions on the Internet. My search apparently turn to this link to keep the device awake:
https://developer.android.com/training/scheduling/wakelock
But this solution did not work properly for me and the android system block my application in background when the screen is locked. If the screen is unlocked, the application works properly.
Moreover, I do not understand the problem. Probably, the android energy saver block my application.
Have you any idea to fix this? And if not, how to use the wakeLock?
Thank you :)
there is two way to hold the CPU running (on) even after screen lock
1. Wake Lock
wake lock hold cpu on as long as you acquire it. for acquiring wakelock
first define this permission in manifest
<uses-permission android:name="android.permission.WAKE_LOCK" />
then you can have wakelock like this
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakelockTag");
wakeLock.acquire();
and after your task is done you can release wakelock like this
wakeLock.release();
UPDATE
WakefulBroadcastReceiver is deprecated now with android 8.0
2. WakefulBroadcastReceiver
it's a special broadcast receiver that takes care everything about wakelock
for using this first define in manifest
<receiver android:name=".WakefulReceiver"/>
and then same as we use our service you can do
public class WakefulReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
startWakefulService(context, your_service_intent);
}
}
here you can see all we have to do is start service using startWakefulService
this will take care everything

How to keep Android awake in background when screen is off

I runs web server in my rooted Android Tablet. I setup it for web development. I created a home network by this Android server. but when the screen of the tab turns off, the server also stop working & again start working when i turn on the screen. but its not possible to turn on the screen for long time. I can be harmful for my tablet. Is there any way to keep awake my tablet when the screen is turn off so that my server can work properly in Background.
please help
You can run a service in the background and acquire lock like this :
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK,
"MyWakelockTag");
wakeLock.acquire();
and return onStartCommand START_STICKY. To release the wake lock, call wakelock.release().
Do not forget to put the permission in the manifest file :
<uses-permission android:name="android.permission.WAKE_LOCK" />
Or you can use this app to prevent phone from sleeping :
https://play.google.com/store/apps/details?id=nl.syntaxa.caffeine

How can I prevent Galaxy S3 from stopping my app when turning idle?

I am currently updating my Android app with Samsung Galaxy S3 and was shocked that I couldn't stop the phone pausing my app when turning idle. With the Galaxy S2 of our department there doesn't occur this particular problem, if the screen goes black the app still streams data to the sd-card and over the wifi-network. The S3 stops any data-stream.
I tried now fiddling with the energy- and display-settings but I have no solution to the problem so far. My Internet-search was not succesfull either.
Possible solutions are rooting the new phone and thus making advanced settings visible
or increasing the time-out (which i dont like so much as a solution).
Do you have any ideas how to solve the issue or general input that might enlighten me?
Thnx!
BTW: here is the app in question (no ad):
Google Play Link
I have an app which needs to do something similar (it's a running trainer, so it needs to keep talking while the user keeps their phone in their pocket for half an hour or so.)
First off, a caveat for other people reading: don't do this if you don't have to. If you only need to do something periodically, rather than continuously, consider using AlarmManager, which can wake the phone up from sleep every now and again to do something, so won't hit the user's battery so hard.
But, if you're sure you need to keep the phone awake, you need to use a WakeLock. Here's roughly what I do in my service's onStartCommand:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK;, "RunClockService");
mWakeLock.acquire();
...where mWakeLock is an instance variable of type PowerManager.WakeLock. PARTIAL_WAKE_LOCK keeps the CPU running, but doesn't keep the screen on. The "RunClockService" tag is just used for debugging, according to the documentation. Change it to your class name.
Then, when I finish needing to keep the phone awake:
mWakeLock.release();
You'll also need to add WAKE_LOCK permission to your AndroidManifest.xml:
<uses-permission android:name="android.permission.WAKE_LOCK"/>

unlocking a screen via code in android

How do I unlock phone screen when some event happens?I tried the following code but it does not unlock the screeen . By unlock I mean bypass PIN or pattern
Am using following code and its get triggered when a sms is received.
private void unlockScreen(Context context){
Log.d("dialog", "unlocking screen now");
PowerManager powermanager = ((PowerManager)context.getSystemService(Context.POWER_SERVICE));
WakeLock wakeLock = powermanager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
wakeLock.acquire();
Window wind = DialogActivity.this.getWindow();
wind.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
wind.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
wind.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
}
Screen is powered on but the user has to enter PIN/pattern.How do I get over it?
Straight from the android API Site for disableKeyguard():
Disable the keyguard from showing. If the keyguard is currently
showing, hide it. The keyguard will be prevented from showing again
until reenableKeyguard() is called. A good place to call this is from
onResume() Note: This call has no effect while any DevicePolicyManager
is enabled that requires a password.
Based off that bolded statement I would probably say that you cannot do that without a password. The only way passed that is if you had yourself(app) added to the phone as a device admin, then you could control that from your device admin application of removing the password, wiping it etc.
Source : KeyguardManager.KeyguardLock & DevicePolicyManager
EDIT
I found the source code of the LockPatternUtils (I know it is from older version, but I doubt it has changed much) that is in part pattern locks and it has DevicePolicyManager all over it. I believe it has an internal service running as root in the system that does all the work. So without being a device admin, you do not even have authority to unlock the phone when it has a security setting for it.

Categories

Resources