vibrate phone in background
-
29-06-2021 - |
문제
The more I read, more confuse I get. I'm trying to make one simple program where the phone vibrates every minute and updates a text field.
My application runs fine when display is on and as it should, stops working when display is off.
First I thought I should use Service, but then there is this class called AlarmManager and also TimerTask and Timer.
What is the best class/method to use so that my apps makes my phone vibrate every minute?
해결책
Not sure if you want this to be done when the app (activity) is opened. If this is the case you can create a thread that sleeps for one minute (not to block the UI thread) and then vibrates. If you want to do this in the background then I suggest you use a Service.
I hope this helps.
다른 팁
Use Alarm Manager
and schedule recurring alarm each minute (assuming that's what you really want). Your screen does not need to be on for this to work, even more, it should not if it wasn't as you do not need screen for vibration. Mind, your app needs to use right permission:
<uses-permission android:name="android.permission.VIBRATE"/>
or security exception will be thrown when you try to use vibration without that,