仅在我自己的应用程序中发送和接收短信,而不是使用本机SMS API在Android中的本机消息应用程序

我在我的应用程序中有一个要求,我需要在应用程序中集成消息传递.我想使用Android本机SMS API发送消息并接收它们.主要的挑战是我不想在消息应用程序中显示收到的消息.所有消息都应该打开,只能从应用程序发送.

我已经尝试在我的广播接收者接受以下意图:

<intent-filter>
    <action android:name="android.provider.telephony.SMS_RECEIVED"></action>
</intent-filter>

但是当消息来到我的应用程序在同一个是由本机消息应用程序接收,我不想要.

我也尝试在模拟器的特定端口发送数据消息,它发送的是messgae,但是没有被我的应用程序接收,还有其他模拟器上的Message native应用程序.

意图过滤器是:

<intent-filter> 
        <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
        <data android:port="8901"/>
        <data android:scheme="sms"/>
</intent-filter>

我使用给定端口的sendDataMessage()函数.

是否可能以更好和安全的方式发送消息,以便在Android中无法使用本机SMS API窃取您的数据?如果没有什么替代我可以去执行相同的.

最佳答案
这是我实现的,它的工作就像我想要的那样.

输入电话号码和短信后调用此方法.

private static final int MAX_SMS_MESSAGE_LENGTH = 160;
private static final int SMS_PORT = 8901;
private static final String SMS_DELIVERED = "SMS_DELIVERED";
private static final String SMS_SENT = "SMS_SENT";

private void sendSms(String phonenumber,String message) { 

    SmsManager manager = SmsManager.getDefault();
    PendingIntent piSend = PendingIntent.getBroadcast(this, 0, new Intent(SMS_SENT), 0);
    PendingIntent piDelivered = PendingIntent.getBroadcast(this, 0, new Intent(SMS_DELIVERED), 0);

byte[] data = new byte[message.length()];

for(int index=0; index<message.length() && index < MAX_SMS_MESSAGE_LENGTH; ++index)
    {
       data[index] = (byte)message.charAt(index);
    }

manager.sendDataMessage(phonenumber, null, (short) SMS_PORT, data,piSend, piDelivered);

}



 private BroadcastReceiver sendreceiver = new BroadcastReceiver()
 {
         @Override
         public void onReceive(Context context, Intent intent)
         {
                 String info = "Send information: ";

                 switch(getResultCode())
                 {
                         case Activity.RESULT_OK: info += "send successful"; break;
                         case SmsManager.RESULT_ERROR_GENERIC_FAILURE: info += "send failed, generic failure"; break;
                         case SmsManager.RESULT_ERROR_NO_SERVICE: info += "send failed, no service"; break;
                         case SmsManager.RESULT_ERROR_NULL_PDU: info += "send failed, null pdu"; break;
                         case SmsManager.RESULT_ERROR_RADIO_OFF: info += "send failed, radio is off"; break;
                 }

                 Toast.makeText(getBaseContext(), info, Toast.LENGTH_SHORT).show();

         }
 };

 private BroadcastReceiver deliveredreceiver = new BroadcastReceiver()
 {
         @Override
         public void onReceive(Context context, Intent intent)
         {
                 String info = "Delivery information: ";

                 switch(getResultCode())
                 {
                         case Activity.RESULT_OK: info += "delivered"; break;
                         case Activity.RESULT_CANCELED: info += "not delivered"; break;
                 }

                 Toast.makeText(getBaseContext(), info, Toast.LENGTH_SHORT).show();
         }
 };

您的收件人的邮件应如下所示:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.SmsMessage;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

public class MySMSReceiver extends BroadcastReceiver {

String action,from,message;

@Override
public void onReceive(Context context, Intent intent) {


     action=intent.getAction();

    Bundle bundle = intent.getExtras();        
    SmsMessage[] msgs = null;


        if(null != bundle)
        {
            String info = "Binary SMS from ";
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];

            byte[] data = null;

            for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
                info += msgs[i].getOriginatingAddress();                    
                info += "\n*****BINARY MESSAGE*****\n";
                from= msgs[i].getOriginatingAddress(); 
                data = msgs[i].getUserData();

                for(int index=0; index<data.length; ++index) {
                   info += Character.toString((char)data[index]);
                   message += Character.toString((char)data[index]);
                }
            }

        }

    Intent showMessage=new Intent(context, AlertMessage.class);
    showMessage.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    showMessage.putExtra("from", from);
    showMessage.putExtra("message", message);
    context.startActivity(showMessage);

}

}

我创建了一个简单的活动AlertMessage.java来显示收到的消息.

我在Manifest中注册我的广播接收器的方式:

 <receiver android:name=".MySMSReceiver">
     <intent-filter>
        <action android:name="android.intent.action.DATA_SMS_RECEIVED" /> 
        <data android:scheme="sms" /> 
        <data android:port="8901" />      
     </intent-filter> 
 </receiver>

这里提到的端口必须与发送消息的sendSMS()方法中指定的端口相同.

更新:

Github工作项目库

https://github.com/pyus-13/MySMSSender

转载注明原文:仅在我自己的应用程序中发送和接收短信,而不是使用本机SMS API在Android中的本机消息应用程序 - 代码日志