2011-08-22 124 views
10

我很困惑。我试图配置我的应用程序来响应SD卡变得可用/离线,但我的广播接收器永远不会被调用!MEDIA_MOUNTED广播未收到

我可以看到该事件被广播和其他应用响应:

08-21 23:43:04.405: DEBUG/Ringer(275): -- intent.getAction() =android.intent.action.MEDIA_MOUNTED 

而我的表现已经接收声明:

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

我的接收器具有的onReceive方法:

public class Test extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.d("#########", "##############################################################"); 
     Log.d("#########", "Obligitory snarky and/or funny logging comment..."); 
     Log.d("#########", "##############################################################"); 
    } 
} 

但& ^%$'ing事情不会导致Test.onReceiv e()开火。有什么想法吗?

回答

39

你不可能是认真的。显然我需要为数据类型添加一个额外的过滤器。

离开了“未来小子”的答案了...

<receiver android:name=".Test" android:enabled="true"> 
    <intent-filter> 
     <action android:name="android.intent.action.MEDIA_MOUNTED" /> 
     <data android:scheme="file"/> 
    </intent-filter> 
</receiver> 
+1

看起来官方文档缺少该方案,这肯定是令人讨厌的,因为他们的代码实际上不工作。 http://developer.android.com/reference/android/os/Environment.html – smith324

+1

对于动态链接接收者的人来说,你可以在你的IntentFilter实例上调用addDataScheme(“file”)。 – dhakim

8

您尝试添加<data android:scheme="file" />标签在<intentfilter>,否则做了登记在运行时。
在运行时,创建广播接收器的一个对象并将它传递给registerReceiver(obj)

+0

谢谢,我在发布前几分钟就想出了它。 –

+0

然后您应该将此添加到回答的问题列表中。 – Ronnie

+1

嗯,它是......并且在你提交答案之前。我在15分钟后添加了一个答案。 –