我正在写一个警报应用程序。我有一个BroadcastReceiver
,我正在使用AlarmService
来处理警报。
在BroadcastReceiver
中,我想要执行网络通话,然后如果满足某些条件,请使用SoundPool
播放声音。
但是,在运行BroadcastReceiver
的线程中不允许进行网络调用,因此我得到NetworkOnMainThreadException
。我尝试使用IntentService
,它在后台线程上执行,这让我解决了网络调用问题。但是,我还没有设法发出声音,并得到:
W/MessageQueue: Handler (android.media.SoundPool$EventHandler) {d64695a} sending message to a Handler on a dead thread
java.lang.IllegalStateException: Handler (android.media.SoundPool$EventHandler) {d64695a} sending message to a Handler on a dead thread
我的理解是播放声音的SoundPool
以异步方式播放,并且线程已经死亡。
如何才能从BroadcastReceiver
开始执行网络呼叫并调用SoundPool
?
更新
这是代码的第一个版本,我试图在BroadcastReceiver
内进行HTTP调用。之后是我收到的例外。请注意,尽管此处从未到达播放声音的声部,但如果我跳过HTTP呼叫,它确实有效。
package com.marksoft.alarm.alarm.events;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioAttributes;
import android.media.SoundPool;
import com.marksoft.alarm.R;
import com.marksoft.alarm.alarm.data.IAlarmRepository;
import com.marksoft.alarm.alarm.data.InMemoryAlarmRepository;
import com.marksoft.alarm.backend.myApi.model.Prediction;
import com.marksoft.alarm.tfl.TflService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
public class BusStopCheckReceiver extends BroadcastReceiver {
private static final Logger LOG = LoggerFactory.getLogger(com.marksoft.alarm.alarm.events.BusStopCheckReceiver.class);
private SoundPool soundPool;
private TflService tflService = new TflService();
private IAlarmRepository alarmRepository = InMemoryAlarmRepository.getInstance();
@Override
public void onReceive(Context context, Intent intent) {
// ...
if(shouldSoundAlarm()) {
playSound(context);
}
// ...
}
private boolean shouldSoundAlarm() {
// HTTP call
List<Prediction> predictions = tflService.getBusStopArrivals(alarm.getStopId());
for(Prediction prediction : predictions) {
LOG.info("Checking prediction {}", prediction);
if(...) {
LOG.info("Should sound alarm for prediction {}", prediction);
return true;
}
}
return false;
}
private int playSound(final Context context) {
// Load the sound
AudioAttributes attributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
soundPool = new SoundPool.Builder()
.setAudioAttributes(attributes).build();
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
soundPool.play(sampleId, 1, 1, 1, 0, 1f);
LOG.info("Played sound");
}
});
return soundPool.load(context, R.raw.bell1, 1);
}
}
例外:
09-21 13:53:05.020 3218-3218/com.marksoft.alarm D/AndroidRuntime: Shutting down VM
--------- beginning of crash
09-21 13:53:05.021 3218-3218/com.marksoft.alarm E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.marksoft.alarm, PID: 3218
java.lang.RuntimeException: Unable to start receiver com.marksoft.alarm.alarm.events.BusStopCheckReceiver: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3018)
at android.app.ActivityThread.-wrap18(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1544)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
at com.android.org.conscrypt.Platform.blockGuardOnNetwork(Platform.java:300)
at com.android.org.conscrypt.OpenSSLSocketImpl.shutdownAndFreeSslNative(OpenSSLSocketImpl.java:1194)
at com.android.org.conscrypt.OpenSSLSocketImpl.close(OpenSSLSocketImpl.java:1189)
at com.android.okhttp.Connection.closeIfOwnedBy(Connection.java:148)
at com.android.okhttp.OkHttpClient$1.closeIfOwnedBy(OkHttpClient.java:77)
at com.android.okhttp.internal.http.HttpConnection.closeIfOwnedBy(HttpConnection.java:137)
at com.android.okhttp.internal.http.HttpTransport.disconnect(HttpTransport.java:135)
at com.android.okhttp.internal.http.HttpEngine.disconnect(HttpEngine.java:573)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.disconnect(HttpURLConnectionImpl.java:134)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.disconnect(DelegatingHttpsURLConnection.java:93)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.disconnect(HttpsURLConnectionImpl.java)
at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:99)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:981)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at com.marksoft.alarm.tfl.TflService.getBusStopArrivals(TflService.java:78)
at com.marksoft.alarm.alarm.events.BusStopCheckReceiver.shouldSoundAlarm(BusStopCheckReceiver.java:70)
at com.marksoft.alarm.alarm.events.BusStopCheckReceiver.onReceive(BusStopCheckReceiver.java:55)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3011)
at android.app.ActivityThread.-wrap18(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1544)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
答案 0 :(得分:2)
是的,您可以启动IntentService,在完成此意向服务后,您可以再次通过sendBroadCast启动broadcastReceiver并添加一些额外的onReceive方法获得额外的匹配然后播放声音。 这样,您就可以同步网络呼叫并播放声音。