【问题标题】:How to detect headphone plug event in "offline" mode [closed]如何在“离线”模式下检测耳机插入事件[关闭]
【发布时间】:2013-04-30 19:58:04
【问题描述】:

如果我的应用程序正在运行,我知道如何检测耳机插入事件。您必须为 ACTION_HEADSET_PLUG 注册广播接收器。但是您无法使用广播接收器的清单声明来捕获此操作。因此,捕获耳机插入事件的唯一选项是后台服务。但它会耗尽电池等。

我检查了一些音乐播放器,发现它们无需任何额外服务即可捕获该事件。他们是怎么做到的?有什么想法吗?

【问题讨论】:

  • “我检查了一些音乐播放器,发现它们无需任何其他服务即可捕获该事件”——说出任何无需运行代码即可捕获该事件的名称。
  • “我检查了一些音乐播放器,发现它们无需任何额外服务即可捕获该事件。”你是怎么想出来的?是因为这些应用程序都没有出现在最近的应用程序中吗?因为如果是这样,那很容易做到。
  • "命名任何能够在没有运行代码的情况下捕获此事件的人。"功放
  • “你是怎么知道的?是因为这些应用程序都没有出现在最近的应用程序中吗?因为如果是这样,那很容易做到。” - 我尝试使用默认应用程序管理器和其他显示电池寿命和正在运行的应用程序的应用程序在正在运行的进程/服务列表中找到它们。
  • @MistaGreen 请参阅下面的解决方案,我已经成功使用了很长一段时间,没有出现电池耗尽的投诉。

标签: android broadcastreceiver


【解决方案1】:

我在商店里有一个应用程序已经三年了,它可以同时监控有线耳机和蓝牙的状态,而且从来没有人抱怨过电池耗电。但那是因为我成功地使用了一个服务和广播接收器来检测来自两者的事件。这是两个关键类:

public class HeadsetStateBroadcastReceiver extends BroadcastReceiver {

    public static final String[] HEADPHONE_ACTIONS = {
        Intent.ACTION_HEADSET_PLUG,
        "android.bluetooth.headset.action.STATE_CHANGED",
        "android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED"
    };

    @Override
    public void onReceive(final Context context, final Intent intent) {

        boolean broadcast = false;

        // Wired headset monitoring
        if (intent.getAction().equals(HEADPHONE_ACTIONS[0]) {
            final int state = intent.getIntExtra("state", 0);
            AudioPreferences.setWiredHeadphoneState(context, state > 0);
            broadcast = true;
        }

        // Bluetooth monitoring
        // Works up to and including Honeycomb
        if (intent.getAction().equals(HEADPHONE_ACTIONS[1])) {
            int state = intent.getIntExtra("android.bluetooth.headset.extra.STATE", 0);
            AudioPreferences.setBluetoothHeadsetState(context, state == 2);
            broadcast = true;
        }

        // Works for Ice Cream Sandwich
        if (intent.getAction().equals(HEADPHONE_ACTIONS[2])) {
            int state = intent.getIntExtra("android.bluetooth.profile.extra.STATE", 0);
            AudioPreferences.setBluetoothHeadsetState(context, state == 2);
            broadcast = true;
        }

        // Used to inform interested activities that the headset state has changed
        if (broadcast) {
            LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("headsetStateChange"));
        }

    }

}

这是我用来注册广播接收器的服务:

public class HeadsetMonitoringService extends Service {

    HeadsetStateBroadcastReceiver headsetStateReceiver;

    @Override
    public void onCreate() {

        headsetStateReceiver = new HeadsetStateBroadcastReceiver();
        final IntentFilter filter = new IntentFilter();
        for (String action: HeadsetStateBroadcastReceiver.HEADPHONE_ACTIONS) {
            filter.addAction(action);
        }

        registerReceiver(headsetStateReceiver, filter);

    }

    @Override
    public int onStartCommand(final Intent intent, final int flags, final int startId) {
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        unregisterReceiver(headsetStateReceiver);
    }

    @Override
    public IBinder onBind(final Intent intent) {
        return null;
    }

}

这是我的清单条目:

    <service
        android:name=".services.HeadsetMonitoringService"
        android:enabled="true"
        android:exported="false" >
        <intent-filter>
            <action android:name="initialiseHeadsetService" />
        </intent-filter>
    </service>

工作原理如下:

我使用启动广播接收器向 HeadsetMonitoringService 发送启动服务消息(您不必这样做,您可以在应用程序启动时这样做)。 HeadsetMonitoringService 反过来注册了一个广播监听器的实例,它监听我感兴趣的所有耳机​​事件——它们保存在 HEADPHONE_ACTIONS 数组中。因为服务是粘性的,所以它会挂起 - 因此广播侦听器也是如此。但是因为服务和广播侦听器都是事件驱动的,所以在耳机状态发生变化之前它们不会消耗任何功率。此外,由于该服务具有粘性,如果它意外死亡,它将被操作系统重新启动。
每当我收到耳机状态更改事件时,我也会触发本地广播,以便感兴趣的活动可以检查新状态并在需要时采取行动。

为了完整起见,我应该指出我使用另一个类(此处未显示)AudioPreferences 将蓝牙和有线耳机状态作为首选项存储,然后可以在我需要知道耳机状态时访问它们。

如果您对蓝牙耳机的状态感兴趣,您的应用程序将需要 android.permission.BLUETOOTH 权限。如果没有,只需从 HEADPHONE_ACTIONS 数组中取出蓝牙相关的操作,并从 onReceive 方法中删除关联的 if 块。

【讨论】:

  • 我已经实现了非常相似的解决方案,但忘记在此处发布。现在一切都很完美。谢谢菲尔!
猜你喜欢
  • 2010-10-26
  • 2012-01-18
  • 2017-04-24
  • 1970-01-01
  • 2013-05-08
  • 2011-04-04
  • 2013-11-11
  • 2017-03-08
  • 1970-01-01
相关资源
最近更新 更多