0

我已经成功使用了几次IntentService,但是在当前的应用程序中,我无法启动BroadcastReceiver的onReceive()。我在 Intent 的 IntentService 操作和 IntentFilter 中有确切的字符串,它与 BroadcastReceiver 一起在 onResume 中注册。

    filter = new IntentFilter("com.currencyconverter2.intent.action.MESSAGE");
    Log.d("CC2", "intent filter created");
    filter.addCategory(Intent.CATEGORY_DEFAULT);
    Log.d("CC2", "filter category added");
    receiver = new MessageReceiver();
    Log.d("CC2", "receiver instantiated");
}

public void onResume(){
    super.onResume();
    registerReceiver(receiver, filter);
    Log.d("CC2", "receiver registered");
}

public void onPause(){
    super.onPause();
    unregisterReceiver(receiver);
    Log.d("CC2", "receiver unregistered");
}

public class MessageReceiver extends BroadcastReceiver{
    public void onReceive(Context context, Intent intent){
        Log.d(Tag, "entered onReceive...");
        String html = intent.getStringExtra("OUTPUT");
        webView.setBackgroundColor(Color.parseColor("#f4f36f"));
        webView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
    }
}

我还在 Manifest.xml 中指定了服务。

<service android:name=".IntentServiceTest"></service>

我的日志告诉我,intentBroadcast 是从 IntentService 广播的,并且应用程序停在那里。BroadcastReceiver 似乎没有收到它。有什么问题?谢谢!

编辑:

我的意图服务

        Intent broadcastIntent = new Intent();
        Log.d(Tag, "setting intent action");
        broadcastIntent.setAction("com.currencyconverter2.intent.action.MESSAGE");
        Log.d(Tag, "adding intent category");
        broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
        Log.d(Tag, "putting intent extra");
        broadcastIntent.putExtra("OUTPUT", data.text());
        Log.d(Tag, "sending broadcast");
        sendBroadcast(intent);

    }catch(Exception e){
        e.printStackTrace();
        //System.out.println("f");
    }
}

}

编辑2:

发现了错误。

                          Intent broadcastIntent = new Intent();
        Log.d(Tag, "setting intent action");
        broadcastIntent.setAction("com.currencyconverter2.intent.action.MESSAGE");
        Log.d(Tag, "adding intent category");
        broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
        Log.d(Tag, "putting intent extra");
        broadcastIntent.putExtra("OUTPUT", data.text());
        Log.d(Tag, "sending broadcast");
        sendBroadcast(intent);

我应该有 sendBroadcast(broadcastIntent); 反而。

4

2 回答 2

1

IntentService用于执行任务并将其关闭,但与您的代码一样,您似乎正在使用较长的运行时间,请使用Service而不是IntentService

于 2012-07-29T08:03:01.927 回答
0

检查您的 AndroidManifest.xml:

<receiver android:name=".MessageReceiver">
<intent-filter>
<action android:name="com.currencyconverter2.intent.action.MESSAGE" />
<category android:name="android.intent.category.Default" />
</intent-filter>
</receiver>

^-^

于 2012-07-29T08:34:45.527 回答