我已经成功使用了几次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); 反而。