Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了仅在我自己的应用程序中发送和接收短信,而不是使用本机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:scheR_946_11845@e="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:scheR_946_11845@e="sms" /> 
        <data android:port="8901" />      
     </intent-filter> 
 </receiver>

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

更新:

Github工作项目库

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

大佬总结

以上是大佬教程为你收集整理的仅在我自己的应用程序中发送和接收短信,而不是使用本机SMS API在Android中的本机消息应用程序全部内容,希望文章能够帮你解决仅在我自己的应用程序中发送和接收短信,而不是使用本机SMS API在Android中的本机消息应用程序所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。