0

我已经搜索了互联网和 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>

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

4

2 回答 2

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" />

它也应该工作。

希望这可以帮助

于 2017-12-16T12:26:38.033 回答
1
android.provider.telephony.SMS_RECIEVED

将其更改为:

android.provider.Telephony.SMS_RECEIVED

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

于 2017-12-16T12:15:09.583 回答