반응형
java.lang.RuntimeException : 처리기 (android.os.Handler)가 죽은 스레드에서 처리기로 메시지를 전송
내 앱에서 SMS를 보내기 위해 IntentService를 사용하고 있습니다.
@Override
protected void onHandleIntent(Intent intent) {
Bundle data = intent.getExtras();
String[] recipients = null;
String message = getString(R.string.unknown_event);
String name = getString(R.string.app_name);
if (data != null && data.containsKey(Constants.Services.RECIPIENTS)) {
recipients = data.getStringArray(Constants.Services.RECIPIENTS);
name = data.getString(Constants.Services.NAME);
message = data.getString(Constants.Services.MESSAGE);
for (int i = 0; i < recipients.length; i++) {
if(!StringUtils.isNullOrEmpty(recipients[i])) {
try {
Intent sendIntent = new Intent(this, SMSReceiver.class);
sendIntent.setAction(Constants.SMS.SEND_ACTION);
PendingIntent sendPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, sendIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent deliveryIntent = new Intent(this, SMSReceiver.class);
deliveryIntent.setAction(Constants.SMS.DELIVERED_ACTION);
PendingIntent deliveryPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, deliveryIntent, PendingIntent.FLAG_UPDATE_CURRENT);
SmsManager.getDefault().sendTextMessage(recipients[i].trim(), null, "[" + name + "] " + message, sendPendingIntent, deliveryPendingIntent);
} catch (Exception e) {
Log.e(TAG, "sendTextMessage", e);
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
MainActivity.instance.writeToLogFile(e.getMessage(), System.currentTimeMillis());
}
}
}
}
}
앱을 실행할 때 다음 오류가 발생합니다.
W/MessageQueue(7180): Handler (android.os.Handler) {42586468} sending message to a Handler on a dead thread
W/MessageQueue(7180): java.lang.RuntimeException: Handler (android.os.Handler) {42586468} sending message to a Handler on a dead thread
W/MessageQueue(7180): at android.os.MessageQueue.enqueueMessage(MessageQueue.java:294)
W/MessageQueue(7180): at android.os.Handler.enqueueMessage(Handler.java:618)
W/MessageQueue(7180): at android.os.Handler.sendMessageAtTime(Handler.java:587)
W/MessageQueue(7180): at android.os.Handler.sendMessageDelayed(Handler.java:558)
W/MessageQueue(7180): at android.os.Handler.post(Handler.java:323)
W/MessageQueue(7180): at android.widget.Toast$TN.hide(Toast.java:367)
W/MessageQueue(7180): at android.app.ITransientNotification$Stub.onTransact(ITransientNotification.java:55)
W/MessageQueue(7180): at android.os.Binder.execTransact(Binder.java:351)
W/MessageQueue(7180): at dalvik.system.NativeStart.run(Native Method)
내 SMSReceiver가 다른 클래스에 있습니다. 이 문제를 어떻게 해결할 수 있습니까? 감사; Eyal.
여기서 문제 Toast
는 .NET Framework에서 관리하는 스레드 내부를 만들고 있다는 것입니다 IntentService
. 시스템은 Handler
이 스레드와 관련된를 사용하여 Toast
.
먼저는 Toast
올바르게 표시되지만 시스템이이를 숨기려고 할 때 onHandleIntent
메서드가 완료된 후 Toast
생성 된 스레드 가 더 이상 유효하지 않기 때문에 "죽은 스레드의 처리기에 메시지 보내기"오류가 발생 합니다. , 및이 Toast
사라지지 않습니다.
이를 방지하려면 Toast
메인 스레드에 메시지를 게시하는 것을 보여 주어야합니다 . 예를 들면 다음과 같습니다.
// create a handler to post messages to the main thread
Handler mHandler = new Handler(getMainLooper());
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();
}
});
IntentService에 토스트 를 표시합니다 . 이 코드를 시도하십시오 ..
@Override
public void onCreate() {
super.onCreate();
mHandler = new Handler();
}
@Override
protected void onHandleIntent(Intent intent) {
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(MyIntentService.this, "Test", Toast.LENGTH_LONG).show();
}
});
}
출처 : -https : //stackoverflow.com/a/5420929/4565853
조건을 확인해야한다고 생각합니다.
mHandler.getLooper().getThread().isAlive()
응용 프로그램은이 오류에 대해 경고합니다. 물론 그것은 대부분 치명적이지 않습니다. 그러나이 핸들러를 많이 사용하는 경우 이러한 경고로 인해 애플리케이션 속도가 느려집니다.
반응형
'developer tip' 카테고리의 다른 글
자바 : Enum 대 Int (0) | 2020.12.10 |
---|---|
줄 바꿈으로 분할하도록 IFS를 설정할 때 백 스페이스를 포함해야하는 이유는 무엇입니까? (0) | 2020.12.10 |
이전 버전의 Android Studio에서 설정을 어디에서 가져 오나요? (0) | 2020.12.10 |
각도 필터를 조건부로 만들기 (0) | 2020.12.10 |
WPF에서 중첩 요소 스타일 지정 (0) | 2020.12.10 |