Intenservice中有什么问题
-
13-12-2019 - |
题
我已经成功使用了几次intenservice,但在当前的应用程序中,我无法让broadcastreceiver的onreceive()开始。我在IntentsService和IntentFilter中有精确的字符串,在onresume与Broadcastreceiver一起注册。
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是从IntenserVice播出的,并且该应用程序停止了。BroadcastReceiver似乎没有收到它。怎么了?谢谢!
编辑:
我的IntensiveService
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(广播节);而是。
解决方案
检查您的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>
.
^ - ^
不隶属于 StackOverflow