Building a Widget to Silence an Android Phone

One of the key parts of Android is the ability to customize your experience through components like widgets. In this tutorial I’ll go over creating a widget to silence a phone with one tap, and in Lollipop (at least with the Nexus 4) it will place the phone into priority mode and silence the ringer, allowing the user to still get their notifications without a vibration. All code for this app can be found on GitHub.


The first thing we’re going to want to do is set up our manifest. Under the applications node, we’ll add a receiver and service.

  1. <receiver android:name=".SilenceRingerWidget">
  2.     <intent-filter>
  3.         <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
  4.     </intent-filter>
  5.     <meta-data android:name="android.appwidget.provider"
  6.         android:resource="@xml/silence_ringer_widget" />
  7. </receiver>
  9. <service android:name=".SilenceRingerService" />

SilenceRingerService is responsible for the actual background work that our widget performs. The receiver is an extension of AppWidgetProvider, which is a special extension of BroadcastReceiver for app widgets. This allows it to be set to listen for APPWIDGET_UPDATEfrom the system. The meta-data provided is an appwidget-provider xml file that simply sets the layout for the widget, refresh rate and size.

  1. @Override
  2. public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
  3.     context.startService(new Intent(context, SilenceRingerService.class));
  4. }

SilenceRingerService is where the bulk of this widget is handled. onStartCommand silences the phone, builds the remote view and then kills the service until the widget button is pressed again.

  1. @Override
  2. public int onStartCommand(Intent intent, int flags, int startId) {
  4.     silencePhone();
  5.     RemoteViews views = buildViews();
  6.     updateWidget( views );
  8.     stopSelf();
  9.     return START_NOT_STICKY;
  10. }

silencePhone() uses the AudioManager to silence the device, and because of Lollipop we also include a thread that starts up after a second and does the same action a second time, because Lollipop first places the device into Priority mode without silencing the device, then silences the ringer after the second setRingerMode call.

  1. private void silencePhone() {
  2.     setPriorityAndSilence();
  3.     new Thread( new Runnable() {
  4.         @Override
  5.         public void run() {
  6.             try {
  7.                 Thread.sleep( 1000 );
  8.             } catch( InterruptedException e ) {
  10.             }
  12.             setPriorityAndSilence();
  14.         }
  15.     } ).run();
  16. }
  18. private void setPriorityAndSilence() {
  19.     AudioManager audioManager;
  20.     audioManager = (AudioManager) getBaseContext().getSystemService( Context.AUDIO_SERVICE );
  21.     audioManager.setRingerMode( AudioManager.RINGER_MODE_SILENT );
  22. }

The last part of onStartCommand that we need to look at has to do with RemoteViews. We simply grab the button from our layout file and set a pending intent to it that will start up SilenceRingerService again, and associate it with our widget.

  1. private RemoteViews buildViews() {
  2.     RemoteViews views = new RemoteViews( getPackageName(), R.layout.widget_silence_ringer );
  3.     PendingIntent silenceIntent = PendingIntent.getService( this, 0, new Intent( this, SilenceRingerService.class ), 0 );
  4.     views.setOnClickPendingIntent(, silenceIntent );
  5.     return views;
  6. }
  8. private void updateWidget( RemoteViews views ) {
  9.     AppWidgetManager manager = AppWidgetManager.getInstance( this );
  10.     ComponentName widget = new ComponentName( this, SilenceRingerWidget.class );
  11.     manager.updateAppWidget( widget, views );
  12. }

Now we have a simple widget put together that can be expanded on to fit other situations that may come up for you as a developer. Enjoy!

Originally published on


Author: Paul Trebilcox-Ruiz

Scroll to Top