在Windows中,SendMessage将阻止调用线程,直到处理完msg。 但是在Android中,似乎sendMessage返回只是将msg放入消息队列中。 那么,有没有办法与Android中的Windows SendMessage做同样的事情?
答案 0 :(得分:0)
没有!
但是,您可以提供两个PendigIntents,这些内容将在发送和传递消息时被触发
当调用sendTextMessage传递这些PendigIntents时:
//Get SmsManager
smsManager = SmsManager.getDefault();
//Define PendingIntent for sent
PendingIntent piSent = PendingIntent.getBroadcast(context, 0,new Intent("com.yourApp.SMS_SENT"),
PendingIntent.FLAG_UPDATE_CURRENT);
//Define PendingIntent for delivered
PendingIntent piDelivered = PendingIntent.getBroadcast(context, 0,new Intent("com.yourApp.SMS_DELIVERED"),
PendingIntent.FLAG_UPDATE_CURRENT);
//Send the message
smsManager.sendTextMessage(phoneNumber, null, messageText, piSent, piDelivered);
定义两个BroadcastReceivers来处理PendingIntents:
BroadcastReceiver for Sent:
public class SentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()){
case Activity.RESULT_OK:
Toast toast = Toast.makeText(context, "Message Sent",Toast.LENGTH_SHORT);
toast.show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
toast = Toast.makeText(context, "Generic failure",Toast.LENGTH_LONG);
toast.show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
toast = Toast.makeText(context, "No service",Toast.LENGTH_LONG);
toast.show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
toast = Toast.makeText(context, "Null PDU", Toast.LENGTH_LONG);
toast.show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
toast = Toast.makeText(context, "Radio off", Toast.LENGTH_LONG);
toast.show();
break;
}
}
}
BroadcastReceiver for Delivered:
public class DeliveredReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()){
case Activity.RESULT_OK:
Toast toast = Toast.makeText(context, "Message Delivered", Toast.LENGTH_SHORT);
toast.show();
break;
case Activity.RESULT_CANCELED:
toast = Toast.makeText(context, "Message not Delivered", Toast.LENGTH_LONG);
toast.show();
break;
}
}
}
现在只需要注册这些接收器即可使用它们。
答案 1 :(得分:0)
我可能没有清楚地提到我的问题。我真正想要的是让Handler.sendMessage(msg)阻塞调用线程,直到Handgate.handleMessage()处理了msg。 在搜索并阅读有关wait()和notify()的内容后,我只需使用信号量即可实现... 但无论如何,谢谢大家,圣诞快乐。 : - )