如何在不使您的应用程序默认消息应用程序的情况下将文本SMS消息发送到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>

短信已成功发送并传递到手机,但默认消息应用程序正在接收短信我的应用程序未收到短信。

首先,您需要阅读接收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>

正如您在带有DATA_SMS_RECEIVEDintent-filter中看到的那样,如果您希望只有您的应用程序能够查看消息而不是任何其他应用程序,并且它不会进入默认的消息传递应用程序收件箱,您还可以添加端口号。 但我不确定您是否可以使用 android 发送Data_sms并向其添加端口号,因为根据我的经验,这是由我的服务器而不是 Android 应用程序完成的。 因此,如果您无法通过特定端口发送,则可以删除:

<data
android:port="1396"
android:scheme="sms" />

它也应该有效。

希望这有帮助

android.provider.telephony.SMS_RECIEVED

将其更改为:

android.provider.Telephony.SMS_RECEIVED

或者,从文档中复制并粘贴值。

最新更新