我已经搜索了互联网和 stackoverflow,并且有很多类似的问题,但没有一个可以帮助我解决我的问题。我有一个发送和接受文本消息的 android 应用程序(不用于消息传递)。我使用以下代码发送短信。
void sendSMS(Context context, final String smsTo,final String message) {
mContext=context;
String SMS_SENT = "SMS SENT";
PendingIntent sentPendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent(SMS_SENT), 0);
String SMS_DELIVERED = "SMS DELIVERED";
PendingIntent deliveredPendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent(SMS_DELIVERED), 0);
//for sms SENT
mContext.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(context, "SMS sent successfully", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(context, "Generic failure cause", Toast.LENGTH_SHORT).show();
sendMessage.sendHTTP(mContext,smsTo, message);
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(context, "Service is currently unavailable", Toast.LENGTH_SHORT).show();
sendMessage.sendHTTP(mContext,smsTo, message);
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(context, "No pdu provided", Toast.LENGTH_SHORT).show();
sendMessage.sendHTTP(mContext,smsTo, message);
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(context, "Radio was explicitly turned off", Toast.LENGTH_SHORT).show();
sendMessage.sendHTTP(mContext,smsTo, message);
break;
}
}
}, new IntentFilter(SMS_SENT));
//for sms Receive
mContext.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(mContext.getApplicationContext(), "SMS delivered", Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(mContext.getApplicationContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
break;
}
}
},new IntentFilter(SMS_DELIVERED));
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(smsTo, null, message, sentPendingIntent, deliveredPendingIntent);
}
我也有一个接收器
public class SMSReceive extends BroadcastReceiver {
private Context mContext;
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle=intent.getExtras();
mContext=context;
SmsMessage[] smsMessage=null;
String stringMessage=null;
if(bundle!=null)
{
Toast.makeText(context.getApplicationContext(),"message recieved",Toast.LENGTH_LONG).show();
Object[] pdus= (Object[])bundle.get("pdus");
smsMessage=new SmsMessage[pdus.length];
// For every SMS message received
for (int i=0; i < smsMessage.length; i++) {
smsMessage[i]=SmsMessage.createFromPdu((byte[])pdus[i]);
stringMessage=smsMessage[i].getDisplayOriginatingAddress()+" "+ smsMessage[i].getMessageBody();
}
if(stringMessage!=null) {
//accept the message do something.
}
}
}
}
我还在 AndroidManifest.xml 文件中注册了广播接收器,如下所示
<receiver
android:name=".SMSReceive"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="999">
<action android:name="android.provider.telephony.SMS_RECIEVED"></action>
</intent-filter>
</receiver>
短信已成功发送并传递到手机,但默认消息应用程序正在接收短信我的应用程序没有收到短信。