我正在尝试使用sms管理器发送短信,在发送短信后,它应该广播意图以检查是否在广播接收器中发送了消息。短信是使用intentService发送的。
public class MessengingService extends IntentService {
private SmsManager SM;
private PowerManager powerManager;
private WakeLock wakeLock;
public MessengingService() {
super("messenging service");
}
@Override
protected void onHandleIntent(Intent workIntent) {
powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Simplify");
wakeLock.acquire();
Intent sendIntent = new Intent("Message_sent");
sendIntent.putExtra("phone number", workIntent.getStringExtra("phone number"));
PendingIntent sendingPendingIntent = PendingIntent.getService(
getApplicationContext(),Integer.parseInt(workIntent.getStringExtra("phone number")), sendIntent, 0);
SM = SmsManager.getDefault();
SM.sendTextMessage(workIntent.getStringExtra("phone number"), null, workIntent.getStringExtra("text"), sendingPendingIntent, null);
wakeLock.release();
}
}
我在清单中注册了广播接收器,
<receiver
android:name="hasebou.karim.simplify.MyReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="Message_sent"/>
</intent-filter>
</receiver>
这是我的广播接收器的接收方法
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("Message_sent")){
Log.e("broadcast", "am sittubg doing nothing");
switch(getResultCode()){
case Activity.RESULT_OK: // Sms sent
notifyMessageSent(MainActivity.thisContext,"Message sent to: "+intent.getStringExtra("phone number"));
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE: // generic failure
notifyMessageSent(MainActivity.thisContext,"Failed to send message to: "+intent.getStringExtra("phone number"));
break;
case SmsManager.RESULT_ERROR_NO_SERVICE: // No service
notifyMessageSent(MainActivity.thisContext,"Failed to send message because no service: "+intent.getStringExtra("phone number"));
break;
case SmsManager.RESULT_ERROR_NULL_PDU: // null pdu
notifyMessageSent(MainActivity.thisContext,"Failed to send message to: "+intent.getStringExtra("phone number"));
break;
case SmsManager.RESULT_ERROR_RADIO_OFF: //Radio off
notifyMessageSent(MainActivity.thisContext,"Failed to send message because radio is turned off: "+intent.getStringExtra("phone number"));
break;
}
}
}