2015-04-22 150 views
1

我正在尝试按照this documentationACTION_DOCK_EVENT收听码头更改事件。Android监听ACTION_DOCK_EVENT

我的接收器从未被击中。

我的代码看起来很简单,所以我想知道是否有需要监听docks事件的权限?我是否缺少与ACTION_DOCK_EVENT相关的其他内容?

AndroidManifest.xml中

<receiver android:enabled="true" android:exported="true" android:label="MyReceiver" android:name="path.to.MyReceiver"> 
    <intent-filter> 
    <action android:name="android.intent.action.DOCK_EVENT"/> 
    </intent-filter> 
</receiver> 

MyReceiver.java

public class MyReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
    if (intent.getAction() == Intent.ACTION_DOCK_EVENT) { 
     //Never hit :(
    } 
    } 

} 

我被打开/关闭我的手机变成我的交流电源插座和MacBook Pro测试。我正在使用Android 4.4.4的Moto X(2nd Genration)。

回答

2

码头如见于types of docks附件的一个非常具体的类型:

  • 低端(模拟)台
  • 高端(数字)办公桌

您好,请注意,这些都不符合插入电源。为此,你可能想要monitor changes in charging state使用意图过滤器,如

<receiver android:name=".PowerConnectionReceiver"> 
    <intent-filter> 
    <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/> 
    <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/> 
    </intent-filter> 
</receiver>