5

I had written a small utility app just for my phone, which stopped the annoying carrier provided jingle which played on boot up. I noticed the sound didn't play if I put the phone into silent mode before powering off, so I wrote this little utility to go silent on power down and restore sound on boot. This worked well for a Galaxy S2 on Gingerbread. The entire code is in two classes:

public class OnShutDownReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        AudioManager mgr=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
        mgr.setRingerMode(AudioManager.RINGER_MODE_SILENT);
    }

}

and

public class OnBootReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
      AudioManager mgr=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
      mgr.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
  }
}

The manifest is

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nbt.hush"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <receiver android:name=".OnBootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
         <receiver android:name=".OnShutDownReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.ACTION_SHUTDOWN" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

Now my phone has been upgraded to ICS by my carrier, it no longer works. If I put the phone into silent mode before powering down, the jingle doesn't play. Therefore I suspect that neither receiver is triggered. (I did put some log code in the receivers which didn't show up, but I suspect that because of the timings it might not have been displayed under Gingerbread either.)

Any suggestions please as to why it won't work anymore?

NickT
  • 23,844
  • 11
  • 78
  • 121

1 Answers1

9

If you wound up completely uninstalling and reinstalling the app, the problem is that you have no activity.

Starting with Android 3.1, applications are installed in a "stopped" state, where no broadcast receivers will work until the user manually launches an activity. This is an anti-malware move. I blogged about this ~9 months ago.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I think I understand. So if I add a dummy activity with a Toast or something and run it just the once, it should work thereafter? – NickT Mar 27 '12 at 15:11
  • @NickT: Yes. If you were distributing this app, you could use some tricks to disable the component on earlier versions of Android, so it does not clutter up the launcher. Here is a sample project where I use that trick: https://github.com/commonsguy/cw-advandroid/tree/master/RemoteViews/Plugin – CommonsWare Mar 27 '12 at 15:16
  • Thanks, that works fine now, saves me from social embarrassment when I turn the phone on. (P.S. I'd cut and pasted 'silent' and 'normal' the wrong way round from the real code when I first posted the question) – NickT Mar 27 '12 at 15:32
  • Is this issue happening for pre-loaded applications also? – San Apr 10 '12 at 01:47
  • @SaneeshCS: If by "pre-loaded" you mean "installed on a device before it ships", the device manufacturer should have ways of addressing this problem, but that is outside my area of expertise. – CommonsWare Apr 10 '12 at 12:32
  • it seems to me, that if i do not register the receiver programmatically it never work on ICS emulator, as if the AndroidManifest.xml is useless? – max4ever Nov 23 '12 at 15:45
  • @max4ever: This is covered in my answer, and in the blog post linked to from the answer, should you choose to read them. – CommonsWare Nov 23 '12 at 15:50
  • i did read, and i still say the same, i can't see any android.net.conn.CONNECTIVITY_CHANGE if i declare the reicever in the XML and manually open the application 1000 times, if i do it with code it works, so why is that? is there a list of notifs that don't work by defining them in the androidmanifest.xml? – max4ever Nov 23 '12 at 15:57
  • @max4ever: According to the docs, `CONNECTIVITY_CHANGE` should work from the manifest: http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html That being said, this one is rather unusual to have in the manifest, as usually only running applications care about connectivity changes. – CommonsWare Nov 23 '12 at 16:00