Android广播接收器没有收到意图

Cob*_*0nm 14 android intentfilter android-intent android-broadcast

我有两个应用程序,我试图将一个意图从一个发送到另一个,但意图是永远不会到达,onReceive()但这个问题只是一种方式.第一个应用程序可以发送到第二个,但第二个不能发回信息.我使用不同的意图动作从第二个发送到第一个,但否则它们是相同的.为什么这可能不起作用的任何想法?我已经尝试了我能想到的所有内容,并阅读了我在这里找到的大部分帖子,但无济于事.

它没有崩溃,也没有给我任何迹象表明logcat中发生了什么,它什么也没做.

发送功能

private void sendFinishLog(String ID, String Cond)
{
    Log.d("me", "send finish log");
    Intent logIntent = new Intent();
    logIntent.putExtra("ID", ID);
    logIntent.putExtra("Cond", Cond);
    logIntent.setAction("com.me.intent.finishlog");
    Log.d("me","logIntent : " + logIntent.toString()   
        +logIntent.getExtras().toString());
    sendBroadcast(logIntent);
}
Run Code Online (Sandbox Code Playgroud)

接受上课

public class LogReceiver extends BroadcastReceiver {

    public static ArrayList<LogDataHolder> logData = new ArrayList<LogDataHolder>();
    private boolean found;
    static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    private static String lasttime;
    private static String now = "Boot time";
    @Override
    public void onReceive(Context cont, Intent logIntent) 
    {
        Log.d("me","On receive");
                etc.....
    }
Run Code Online (Sandbox Code Playgroud)

接收应用清单

<!-- for receiving logs  -->
<receiver 
    android:name = "LogReceiver"
    android:enabled="true">
    <intent_filter>
        <action android:name="com.me.intent.finishlog" />
    </intent_filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)

bof*_*edo 7

就像是:

public void onResume() {
    IntentFilter filter = new IntentFilter();
    getActivity().registerReceiver(mLogReceiver,filter);
}
Run Code Online (Sandbox Code Playgroud)

并取消注册:

public void onPause() {
    getActivity().unregsiterReceiver(mLogreceiver);
}
Run Code Online (Sandbox Code Playgroud)

编辑: 我只是想你的LogReceiver类也没有构造函数.你还需要写一个:

private LogReceiver mLogReceiver = new LogReceiver() {
Run Code Online (Sandbox Code Playgroud)

之后你可以使用onReceive方法,就像你已经在代码中使用它一样.

  • 如果你在清单中这样做,你不需要这样做 (4认同)

小智 5

像这样再试一次。可能需要添加类别

<receiver
    android:name = "LogReceiver"
    android:enabled="true">
    <intent-filter>
        <action android:name="com.me.intent.finishlog" />
         <category android:name="android.intent.category.DEFAULT"/> 
    </intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)