Software Development

How can I trigger a vibration via BroadcastReceiver when an incoming call is detected in Android?

KI Asked by Kimberly Harrison · 14-06-2025
0 upvotes 12,466 views 0 comments
The question

I am developing an Android utility app and I need to trigger a custom vibration pattern as soon as the phone starts ringing. I have set up a BroadcastReceiver to listen for the 'android.intent.action.PHONE_STATE' action, but I am struggling to access the Vibrator service from within the onReceive method. Is it possible to initiate a vibration while the phone is in a ringing state, and what specific permissions are required in the Manifest to ensure this works on Android 12 and above?

3 answers

0
DE
Answered on 16-06-2025

Yes, it is entirely possible, but you must ensure your app has the correct permissions. You need both android.permission.VIBRATE and android.permission.READ_PHONE_STATE in your AndroidManifest.xml. Inside the onReceive method, you can access the Vibrator service using context.getSystemService(Context.VIBRATOR_SERVICE). For modern Android versions (API 26+), you should use VibrationEffect.createOneShot() or createWaveform() to define the pattern. Keep in mind that for Android 10+, you also need to handle the "Background Execution" limits, which might require you to start a foreground service if the vibration needs to persist beyond a quick pulse.

0
RY
Answered on 18-06-2025

That is a solid technical breakdown! However, with the privacy changes in more recent Android versions, does the user need to manually grant the "Phone" permission via a runtime prompt before the BroadcastReceiver can even detect the ringing state? I've noticed that even if the permission is in the Manifest, the onReceive code never triggers unless the user has explicitly allowed it in the app settings.

ST 19-06-2025

Ryan, you've raised a critical point for modern software development. Since READ_PHONE_STATE is classified as a "Dangerous Permission," you cannot rely on the Manifest alone. You must implement a runtime check using ActivityCompat.requestPermissions(). If the user hasn't granted it, the system will silently block the broadcast. Furthermore, on Android 11+, if your app targets API 30, you might also need the READ_CALL_LOG permission depending on whether you are trying to access the incoming phone number or just the state change itself.

0
BR
Answered on 21-06-2025

Using the Vibrator class is the right way to go. Just make sure to check if the device actually has a vibrator hardware using vibrator.hasVibrator() to avoid NullPointerExceptions on tablet devices or specific hardware configurations.

KI 22-06-2025

I agree with Brandon. Hardware checks are often overlooked in Android development. I also recommend using vibrator.cancel() once the call state changes to EXTRA_STATE_OFFHOOK or EXTRA_STATE_IDLE. Otherwise, depending on your waveform pattern, the phone might continue to vibrate even after the user has answered the call, which creates a very poor user experience.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session