在ICS BroadcastReceiversICS、BroadcastReceivers

2023-09-05 02:04:24 作者:待到樱花烂漫时

我写了一个小工具,应用程序,只为我的手机,它停止了它发挥在启动时烦人的运营商提供的顺口溜。我发现,如果我把手机进入静音模式关机前的声音没有上场,所以我写了这个小工具去沉默断电和启动恢复声音。这种行之有效的GALAXY S2的姜饼。整个code为两类:

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);
    }

}

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);
  }
}

清单是

<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>

现在我的手机已经升级到ICS通过我的运营商,它不再起作用。如果我把手机进入静音模式断电之前,叮当不玩了。因此,我怀疑,无论是接收器被触发。 (因为它可能没有被姜饼下显示任一时刻。我也把一些日志code中没有露面的接收器,但我怀疑)

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?

推荐答案

如果您清盘完全卸载和重新安装应用程序,问题是你有没有活动。

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

与Android 3.1开始,应用程序安装在一个停止状态,在没有广播接收器将工作,直到用户手动启动的活动。这是一种反恶意软件的举动。 我的博客上讲述这个〜9个月前。

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.