仅在我自己的应用程序中发送和接收短信,而不是使用本机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,new Intent(SMS_SENT),0);
    PendingIntent piDelivered = PendingIntent.getbroadcast(this,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(),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

相关文章

这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内...