在Android中你如何注册接收耳机插头广播? [英] In Android how do you register to receive headset plug broadcasts?

查看:206
本文介绍了在Android中你如何注册接收耳机插头广播?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作在Android 2.1系统,我想,以检测当耳机插入/取出。我是pretty的新机器人。

I am working in Android 2.1, and I want to detect when the headset is plugged in/taken out. I'm pretty new to android.

我觉得它使用的是广播接收器的方式做。我sublcassed这一点,我也把我的Andr​​oidManifest.xml以下。但你必须注册接收somehwere一样,喜欢在活动?我知道有很多关于这个主题的,但我真的不明白他们在说什么。此外,什么是注册在AndroidManifest.xml中与您的活动动态注册的区别?

I think the way to do it is using a Broadcast receiver. I sublcassed this, and I also put the following in my AndroidManifest.xml. But do you have to register the receiver somehwere else, like in the activity? I'm aware there are lots of threads on this, but I don't really understand what they're talking about. Also, what's the difference between registering in AndroidManifest.xml versus registering dynamically in your activity?

<receiver android:enabled="true" android:name="AudioJackReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.HEADSET_PLUG" >
            </action>
        </intent-filter>
    </receiver>

这是类的实现(加进口)

And this was the implementation of the class (plus imports)

public class AudioJackReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Log.w("DEBUG", "headset state received");
}

}

我只是想看看它是否工作,但没有显示出来,当我拔掉/插上耳机,同时运行的应用程序。

I was just trying to see if it works, but nothing shows up when I unplug/plug in the headset while running the application.

编辑:文档不说这个,但有可能是,如果注册在manifest这一个都不行?我能得到我注册的接收器在我的应用程序之一,它响应(或者你有这样做呢?)

the documentation doesn't say this, but is it possible that this one won't work if registered in the manifest? I was able to get it to respond when I registered the receiver in one of my applications (or do you have to do that anyway?)

推荐答案

下面是两个网站可以帮助更详细地解释一下:

Here are two sites that may help explain it in more detail:

  • http://www.grokkingandroid.com/android-tutorial-broadcastreceiver/
  • http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html

您必须确定您的意图;否则将无法访问系统功能。广播接收机;会提醒你,你想​​监听的变化应用。

You have to define your intent; otherwise it won't access the system function. The broadcast receiver; will alert your application of changes that you'd like to listen for.

每一个接收器需要将子类;它必须包括的onReceive()。为贯彻落实的onReceive()你需要创建一个方法,其中将包括两个项目:上下文&放大器; 意图

Every receiver needs to be subclassed; it must include a onReceive(). To implement the onReceive() you'll need to create a method that will include two items: Context & Intent.

更多然后可能是一个服务将是理想的;但你会创建服务,并通过它定义背景。在上下文中;要定义你的意图。

More then likely a service would be ideal; but you'll create a service and define your context through it. In the context; you'll define your intent.

一个例子:

context.startService
      (new Intent(context, YourService.class));

非常简单的例子。然而;您的特定目标是利用全系统的广播。您希望您的应用程序能够获知 Intent.ACTION_HEADSET_PLUG 的。

如何通过订阅清单:

<receiver
    android:name="AudioJackReceiver"
    android:enabled="true"
    android:exported="true" >
    <intent-filter>
        <action android:name="android.intent.action.HEADSET_PLUG" />
    </intent-filter>
</receiver>

或者你可以简单地通过应用程序定义;但。您的特殊要求;需要的用户权限,如果你打算探测蓝牙 MODIFY_AUDIO_SETTINGS

这篇关于在Android中你如何注册接收耳机插头广播?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆