Что может быть неправильно в IntentService
-
13-12-2019 - |
Вопрос
Я успешно использовал Intentservice несколько раз, но в текущем приложении я не могу получить onreceive () Crosstreceiver (), чтобы начать.У меня есть точная строка в действии намерения между интенсивнымСервицем и в целеустремлете, который был зарегистрирован в Onreume вместе с широкозащитником.
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);
}
}
.
Я также указал услугу в Mainest.xml.
<service android:name=".IntentServiceTest"></service>
.
Мой журнал говорит мне, что IntentBroadcast была транслирована от IntEntervice, и приложение остановилось там.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);
.
У меня должно быть отправленное отправление (широковещательное средство);вместо этого.
Решение
Проверьте свой 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>
.
^ - ^
Другие советы
Intentservice Используйте для выполнения задачи и закрыть его, но какPAR Ваш код кажется, что вы используете длительное время выполнения, используйте Сервис вместоиз IntEntervice