如何将文本短信发送到Android中的特定应用程序,而不是制作您的应用程序默认消息应用程序?

问题描述 投票:0回答:2

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

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

android smsmanager android-sms
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" />

它也应该工作。

希望这可以帮助


1
投票
android.provider.telephony.SMS_RECIEVED

改为:

android.provider.Telephony.SMS_RECEIVED

或者,复制并粘贴the documentation中的值。

© www.soinside.com 2019 - 2024. All rights reserved.