I'm using the examples from this site: http://android.konreu.com/developer-how-to/vibration-examples-for-android-phone-development/
So, as you see, I'm using the Vibrator Service. The problem, which also is written on the site, is that the vibration don't work if the screen has timed out.
Anyone has any ideas to fix this? It surely should work as the vibration works on phone calls, alarm etc. even though the screen is black. But maybe I have to wake up the phone before the vibration is called?
See this page about acquiring a wake lock. Hopefully it will do what you want.
Related
Context: I'm under Android 4.0.4.
The code I'm stating is inside an Activity.
The application is a Cordova application, I'm writing this code inside a plugin.
I think I read all the questions here in StackOverflow and the Android docs.
I want to acquire a wakelock, wake up the device, do some stuff and then, when the wakelock is released, i want the screen to turn off again and let the phone sleep.
I have tried several approaches:
I tried several examples found here to wake up the device.
The only way I managed to turn the device on programatically was aquiring a wakelock with the ACQUIRE_CAUSES_WAKEUP flag. This works fine. The problem comes when I release the wakelock. There's no way I can turn the screen off again. It just stays on forever. I tried with all the WAKELOCKS available and with all the FLAGS available.
Note:I'm not totally happy with this approach because as stated in the docs, the only wakelock that is not deprecated is the partial wakelock, which unfortunately can't be used with this flag.
As I couldn't find any way of letting the screen off using the wakelocks functionality I tried to turn it off programmatically.
I think I tried all the examples found here to turn the screen off, and the only one which worked was:
params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = 0;
getWindow().setAttributes(params);
The problem is that using this method I can't turn it on again. When I acquire the wakelock again it just stays off.
Also, when I try to wake up the phone pressing the "home" button it doesnt wake up. It only responds after pressing 3 or 4 times the "power" button. Seems like this approach freezes the phone.
I tried to turn on the screen programmatically before acquiring (and before too) the wakelock, restoring the default brightness value using the same code as above, but It doesn't work.
It's almost a week that I'm struggling with this. Any piece of advice will be appreciated.
I would like to disable sound and vibration for all incoming notifications. I don't mind if phone call vibration gets switched off, too. But the users volume settings for phone calls shouldn't be touched.
Thanks to the AudioManager, muting the sound of notifications is easy:
audioManager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
Disabling the vibration of notifications, however, is harder than expected.
The AudioManager has this deprecated method for vibration settings:
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF)
This, however, seems to have no effect on my Android 5.0 device and I can't find any alternatives. :(
I know that Lollipop has introduced the stuff around "Priority Mode" but I do not want to fiddle with modes. Switching the mode would probably have some other side-effect (based on the users priority preferences).
Any ideas how to mute notification sound and vibration on Android 5.0+?
Since nobody came up with a solution, I'm going to answer my own question.
It seems that there is really no way to achieve what I wanted, at least with the latest Android versions. So I ended up doing the following ugly workaround:
I'm now muting the whole phone (incoming phone calls and notifications) via RINGER_MODE_SILENT but also listen for incoming phone calls via a BroadcastReceiver. As soon as an incoming call is detected, the RingerMode gets reverted to its previous state. This effectively leads to the functionality I wanted to achieve.
I'm, however, extremely unsatisfied with this workaround.
I am aware that this question has been asked already in this site, but I could not find my answer. SO this is what i am trying ot do. My power button in my phone is not working properly. So could is there a way to override my volume up button to unlock my phone.
I am planning to implement broadcast receiver But could not find the appropriate intent-filter. Another alternative that I thought was to run a service when the screen is locked, but I am not quite sure if I can listen to key events.
So if any of the above mentioned is possible, could you hint on how to get it done or suggest another alternative?
No, It do not think so. This can not be done.
There is no broadcast for volume up or down button presently. You can implement a service that will listen for any changes in the volume but it is not recommended to run a service all the time since that consumes both battery and memory.
The only solution that I can think of is using ContentObservers for listening to the changes in the settings such as volume of streams. This answer here will provide you with the code to use ContentObserver to listen for volume up or down. https://stackoverflow.com/a/7017516/1979347
This functionality you speak of has already been done multiple times and can be found on Google Play. However, I'm not sure how they did it, or how much their solution drains the battery.
I just did a test on my G2 (Android 4.2.2), I launched a broadcast monitor, I switched to the homescreen, and I changed the volume from there.
Apparently, it can detect if the volume is turned up only when it changes from silent mode > to Vibration mode > or to Sound mode. Conversely, when pressing the down volume, I think we can detect the change when it goes from the Sound Mode > to Vibration Mode > or to Silent Mode.
This is probably not what you wanted to hear, but I just wanted to confirm what the others were saying. On a non-rooted phone, what they're saying is mostly correct. On a rooted phone however, there are custom ROMs that have solved this issue.
By the way, what brand/model is your phone? On my phone, when the headset gets connected, the phone lights up. So I suppose, one could always do it the way iPhone owners do it for their phones. You'd have to cut the extremity of a head phone jack, coat half of the pin in plastic, and super glue a button on top of it to allow you to easily twist the head.
I read this unable to detect shake event when my phones screen is off in android article. But this question is differ from this.
I created an application which speak (using TextToSpeach API) time when I shake device. For that I have created a "Service" in which "ShakeListener" was registered. While I shake device, it will speak current time.
But this works fine while my device is "ON". When I lock my device, it stops working. Can any one elaborate what is the problem behind?
I think the problem here is that when you lock the device, it goes to sleep mode. And your ShakeListener doesn't receive shake events. The best way to implement what you want would be to register broadcast receiver to receive shake events. But as I know, android doesn't provide such broadcast. Another solution is to acuire a partial WakeLock. It'll prevent android from sleep. But of course it'll cause battery issues. Also take a look on this thread: Android accelerometer not working when screen is turned off
Probably you can not receive this notification until you patch some kernel implementation. Android doesn't broadcast such notification.
I have a service that detects when the display of my Android phone is turned on or off. When turned off, the service calls an activity that uses dispatchKeyEvent to detect when the volume up button has been pressed. Unfortunately, apparently the activity can't do this when the screen is off. (See this post.)
I've noticed, though, that something at some level is detecting that event, since the following LogCat message appears when I press the volume-up button when the screen is off: "CatService: Return current sInstance". The message seems to be device specific, since on a different device something different appears in the log, but I'm really only concerned about the first device.
I've done some research into CatService, but haven't found much and can't figure out how I might be able to use it, or whatever is generating the log message, to detect the volume up button press. I'd appreciate any light that anyone can shed on this.
The only way to keep detecting things like this when the screen is off is to acquire a WakeLock that will allow the screen to turn off, and still let your app function. However, this drains the battery life quite a bit, and should only be used when absolutely necessary.
In this case, you will need a PARTIAL_WAKE_LOCK.