在我的应用程序中,我正在使用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。