我搜索了互联网和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>
短信已成功发送并发送至手机,但默认短信应用正在接收短信,我的应用未收到短信。
答案 0 :(得分:1)
android.provider.telephony.SMS_RECIEVED
将其更改为:
android.provider.Telephony.SMS_RECEIVED
或复制并粘贴the documentation的值。
答案 1 :(得分:1)
首先,您需要获得AndroidManifest
中接收短信的权限:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
以及READ_SMS的运行时权限 那么你的广播接收器:
<receiver android:name=".network.SmsBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<data
android:port="1396"
android:scheme="sms" />
</intent-filter>
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
正如您在intent-filter
DATA_SMS_RECEIVED
中所看到的那样,如果您希望只有您的应用能够查看该消息而不是任何其他应用,那么您还可以添加端口号,并且它也不会进入默认消息传递应用收件箱。
但我不确定你是否可以发送带有android的Data_sms以及添加端口号的部分因为我的经验这是由我的服务器而不是Android应用程序完成的。
因此,如果您无法通过特定端口发送,则可以删除:
<data
android:port="1396"
android:scheme="sms" />
它也应该有用。
希望这会有所帮助