我的应用是关于地理围栏。触发地理围栏时,我需要发送短信。
它几乎完美无缺,但我无法接收广播来检查是否发送了短信。我会自己解释一下:
当用户输入地理围栏时,会调用Geofence Intent Service,并发送短信。它就像一个魅力,但我需要确保短信已被发送,或有任何问题(即没有信号),并尝试再次发送。
所以,首先我尝试以这种方式发送短信:
private void sendsms() {
String SMS_SENT = "SMS_SENT";
final PendingIntent sentPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
// check if SMS has been sent
smsSentReceiver =new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Log.e("SMS", "SMS sent successfully");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Log.e("SMS","Generic failure cause");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Log.e("SMS", "Service is currently unavailable");
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Log.e("SMS", "No pdu provided");
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Log.e("SMS", "Radio was explicitly turned off");
break;
}
}
};
registerReceiver(smsSentReceiver, new Intent());
// Get the default instance of SmsManager
// Send a text based SMS
SmsManager smsMgr = SmsManager.getDefault();
smsMgr.sendTextMessage(towhom, null, customtxt, sentPendingIntent, null);
这样可行,但发送短信后没有任何反应,根本没有记录。
然后我尝试了另一种方式,创建接收器并放入清单:
private smsSentReceiver smsSentReceiver;
private void sendsms() {
final PendingIntent sentPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
smsSentReceiver = new smsSentReceiver();
registerReceiver(smsSentReceiver, new IntentFilter());
// Get the default instance of SmsManager
// Send a text based SMS
SmsManager smsMgr = SmsManager.getDefault();
smsMgr.sendTextMessage(towhom, null, customtxt, sentPendingIntent, null);
这是smsSentReceiver.java
public class smsSentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Log.e("SMS", "SMS sent successfully");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Log.e("SMS","Generic failure cause");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Log.e("SMS", "Service is currently unavailable");
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Log.e("SMS", "No pdu provided");
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Log.e("SMS", "Radio was explicitly turned off");
break;
}
}
}
和Manifest:
<receiver
android:name=".smsSentReceiver"
android:enabled="true"
>
</receiver>
根本没有关于短信状态的记录......
关于我为什么没有获得短信状态的任何想法?
PS:仅在模拟器上测试
编辑:
根据@Mike M.的评论,我已经尝试过的最后一件事就是这样:
private void sendsms() {
final PendingIntent sentPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, smsSentReceiver.class), PendingIntent.FLAG_CANCEL_CURRENT);
SmsManager smsMgr = SmsManager.getDefault();
smsMgr.sendTextMessage(towhom, null, customtxt, sentPendingIntent, null);
但仍然没有...
答案 0 :(得分:1)
一旦IntentService
完成工作,sendTextMessage()
就会自动停止,任何在其上动态注册的接收器也会消失。在您的第一个片段中,接收者很可能在最后一次发送时无法接收广播,因为PendingIntent
呼叫将立即返回,IntentService
将被解雇异步。
正如您在第二个片段中所做的那样,静态注册Receiver类将允许接收器即使在Intent
死亡后也能获得广播,因为生成的Receiver实例将不会被绑定到它。但是,您PendingIntent
使用的new Intent(this, smsSentReceiver.class)
无法正确定位Receiver类。将其更改为"SMS"
。
最后,出于某种原因,logs with certain tags are suppressed和"smsSentReceiver"
标签(确切地说)就是其中之一。将该标记更改为未在其中列出的内容;例如,name=next
。