如何收听服务内的 SMS Delivered 广播?
How do I listen to SMS Delivered broadcast inside of a Service?
我正在尝试从后台服务发送 SMS 并在发送时执行一些操作。
发送短信成功。但是即使发送了 SMS,我的广播接收器的 OnReceive 方法也从未被调用。
我已经用其他操作测试了广播接收器,它工作正常。
例如,添加操作“android.intent.action.SCREEN_OFF”效果很好。
它仅无法与我在 PendingIntent 中发送的自定义操作“SMS_DELIVERED”一起使用。
下面是我的代码:
public class SendMessageService extends Service {
private static final String TAG = "SendMessageService";
public static final String SMS_DELIVERED = "SMS_DELIVERED";
private BroadcastReceiver deliveryBroadcastReceiver = null;
@Override
public void onCreate() {
super.onCreate();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(SMS_DELIVERED);
intentFilter.setPriority(100);
deliveryBroadcastReceiver = new DeliverReceiver();
registerReceiver(deliveryBroadcastReceiver, intentFilter);
Log.d(TAG, "Service onCreate: deliveryBroadcastReceiver is registered.");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle extras = intent.getExtras();
String phoneNumber = extras.getString("PHONE_NUMBER");
String msg = extras.getString("msg");
sendSMS(phoneNumber, msg);
Log.d(TAG, "onStartCommand: Sent SMS");
return START_STICKY;
}
private void sendSMS (String phoneNumber, String msg){
PendingIntent deliveredPI = PendingIntent.getBroadcast(getApplicationContext(), 0,
new Intent(SMS_DELIVERED), 0);
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, msg, null, deliveredPI);
}
public class DeliverReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
Log.d(TAG, "onReceive: Delivered onReceive called");
// Do something here
}
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(deliveryBroadcastReceiver);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
上面的代码实际上没问题。似乎问题是我的电信运营商 (EthioTel) 没有提供可靠的短信发送报告。
我正在尝试从后台服务发送 SMS 并在发送时执行一些操作。
发送短信成功。但是即使发送了 SMS,我的广播接收器的 OnReceive 方法也从未被调用。
我已经用其他操作测试了广播接收器,它工作正常。
例如,添加操作“android.intent.action.SCREEN_OFF”效果很好。
它仅无法与我在 PendingIntent 中发送的自定义操作“SMS_DELIVERED”一起使用。
下面是我的代码:
public class SendMessageService extends Service {
private static final String TAG = "SendMessageService";
public static final String SMS_DELIVERED = "SMS_DELIVERED";
private BroadcastReceiver deliveryBroadcastReceiver = null;
@Override
public void onCreate() {
super.onCreate();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(SMS_DELIVERED);
intentFilter.setPriority(100);
deliveryBroadcastReceiver = new DeliverReceiver();
registerReceiver(deliveryBroadcastReceiver, intentFilter);
Log.d(TAG, "Service onCreate: deliveryBroadcastReceiver is registered.");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle extras = intent.getExtras();
String phoneNumber = extras.getString("PHONE_NUMBER");
String msg = extras.getString("msg");
sendSMS(phoneNumber, msg);
Log.d(TAG, "onStartCommand: Sent SMS");
return START_STICKY;
}
private void sendSMS (String phoneNumber, String msg){
PendingIntent deliveredPI = PendingIntent.getBroadcast(getApplicationContext(), 0,
new Intent(SMS_DELIVERED), 0);
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, msg, null, deliveredPI);
}
public class DeliverReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
Log.d(TAG, "onReceive: Delivered onReceive called");
// Do something here
}
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(deliveryBroadcastReceiver);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
上面的代码实际上没问题。似乎问题是我的电信运营商 (EthioTel) 没有提供可靠的短信发送报告。