2015-04-22 56 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>