【问题标题】:Broadcast Receiver dont receive intent广播接收器不接收意图
【发布时间】:2014-07-21 15:19:53
【问题描述】:

我的一个服务广播了一个意图。

public static final String WHATSAPP_INTENT = "com.timelinestudios.ourapp.WHATSAPPSHARER";
Intent sharerintent

...

 System.out.println("Sending intent");
sharerintent = new Intent(WHATSAPP_INTENT);
sharerintent.putExtra("TRACK_PATH", track.getPath());
getApplicationContext().sendBroadcast(sharerintent);
System.out.println("Intent Sent");

然后我放了一个接收器。,

public class WhatsappSharer extends BroadcastReceiver {
public static final String WHATSAPP_INTENT = "com.timelinestudios.ourapp.WHATSAPPSHARER";
public WhatsappSharer() {
}

@SuppressLint("ShowToast")
@Override
public void onReceive(Context context, Intent intent) {
    // TODO: This method is called when the BroadcastReceiver is receiving
    // an Intent broadcast.
    System.out.println("Intent Received");
    if(intent.getAction().equals(WHATSAPP_INTENT)){
        System.out.println("Intent Received");
        Toast ts = Toast.makeText(context, "Ready to share to whatsapp",Toast.LENGTH_SHORT);
        ts.show();
    }
}

}

我还在 Manifest 中添加了,

<receiver
        android:name="com.timelinestudios.ourapp.WhatsappSharer"
        android:enabled="true"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.timelinestudios.ourapp.WHATSAPPSHARER"></action>
            <category android:name="android.intent.category.DEFAULT" /> 
        </intent-filter>
    </receiver>

在 Logcat 中,我从我的服务中获得了 2 个系统 printlns。, 发送意图 意图发送

但没有收到接收方的系统打印或吐司。所以我想,广播接收器没有得到它。可能是什么问题?

编辑:它在 mainactivity 中动态声明广播接收器时起作用。奇怪。为什么会这样?

【问题讨论】:

  • stackoverflow.com/questions/7770620/… 检查这是否有帮助
  • 这是建议,如果它不起作用,请忽略它,请在您的意图过滤器中删除此行 跨度>
  • @farhan 该行指定何时触发此接收器
  • @all.. 谢谢.. 但没有帮助的 cmets。还有其他的吗?

标签: android android-intent broadcastreceiver


【解决方案1】:

在清单中更改接收者的名称。您的接收器类名称与您在清单中提供的名称不同。

<receiver
        android:name=".WhatsappSharer"
        android:enabled="true"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.timelinestudios.ourapp.WHATSAPPSHARER"></action>
            <category android:name="android.intent.category.DEFAULT" /> 
        </intent-filter>
    </receiver>

【讨论】:

  • 没有。不是这个。 com.timelinestudios.ourapp 是我的包名。因此 eclipse auto 将其添加到名称中。你的建议也意味着同样的。无论如何,为了确认,我尝试了你的建议。没有变化。
【解决方案2】:

您是否从接收器所在的其他应用发送广播?

如果是,删除 android:exported="false"(或将其设置为 true)。

当您将意图过滤器添加到接收器的注册中时,它会默认导出,除非您将其显式设置为 false。

【讨论】:

  • 同一个应用程序..不同的包..我尝试将广播接收器与广播服务放在同一个包中,但没有希望。顺便说一句,它可以在 mainactivity 中动态创建接收器。为什么会这样?
【解决方案3】:

从您的 AndroidManifest.xml 中删除以下行:
&lt;category android:name="android.intent.category.DEFAULT" /&gt;

另外,如果这样做不起作用,请尝试将导出的设置为 true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多