iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android接收和发送短信处理
  • 362
分享到

Android接收和发送短信处理

发送短信Android 2022-06-06 09:06:52 362人浏览 泡泡鱼
摘要

关于短信接收处理方面,当前已经有一些app做的比较好了,比如发给手机发验证码验证的问题,很多app在手机接收到验证码后,不需要输入,就直接可以跳过验证界面,这就是用到了对接收到

关于短信接收处理方面,当前已经有一些app做的比较好了,比如发给手机发验证码验证的问题,很多app在手机接收到验证码后,不需要输入,就直接可以跳过验证界面,这就是用到了对接收到的短信的处理。至于短信的发送,也没什么好说的了。在此也只是附上一个小实例。

效果图:

MainActivity:


import Android.app.Activity; 
import android.app.PendingIntent; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.telephony.SmsManager; 
import android.telephony.SmsMessage; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 
public class MainActivity extends Activity { 
  private TextView sender; 
  private TextView content; 
  private IntentFilter receiveFilter; 
  private MessageReceiver messageReceiver; 
  private EditText to; 
  private EditText msgInput; 
  private Button send; 
  private IntentFilter sendFilter; 
  private SendStatusReceiver sendStatusReceiver; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    sender = (TextView) findViewById(R.id.sender); 
    content = (TextView) findViewById(R.id.content); 
    to = (EditText) findViewById(R.id.to); 
    msgInput = (EditText) findViewById(R.id.msg_input); 
    send = (Button) findViewById(R.id.send); 
    //为接收短信设置要监听的广播 
    receiveFilter = new IntentFilter(); 
    receiveFilter.addAction("android.provider.Telephony.SMS_RECEIVED"); 
    messageReceiver = new MessageReceiver(); 
    reGISterReceiver(messageReceiver, receiveFilter); 
    //为发送短信设置要监听的广播 
    sendFilter = new IntentFilter(); 
    sendFilter.addAction("SENT_SMS_ACTION"); 
    sendStatusReceiver = new SendStatusReceiver(); 
    registerReceiver(sendStatusReceiver, sendFilter); 
    send.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        //发送短信 
        //并使用sendTextMessage的第四个参数对短信的发送状态进行监控 
        SmsManager smsManager = SmsManager.getDefault(); 
        Intent sentIntent = new Intent("SENT_SMS_ACTION"); 
        PendingIntent pi = PendingIntent.getBroadcast( 
            MainActivity.this, 0, sentIntent, 0); 
        smsManager.sendTextMessage(to.getText().toString(), null, 
            msgInput.getText().toString(), pi, null); 
      } 
    }); 
  } 
  @Override 
  protected void onDestroy() { 
    super.onDestroy(); 
    //在Activity摧毁的时候停止监听 
    unregisterReceiver(messageReceiver); 
    unregisterReceiver(sendStatusReceiver); 
  } 
  class MessageReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
      Bundle bundle = intent.getExtras(); 
      //使用pdu秘钥来提取一个pdus数组 
      Object[] pdus = (Object[]) bundle.get("pdus"); 
      SmsMessage[] messages = new SmsMessage[pdus.length]; 
      for (int i = 0; i < messages.length; i++) { 
        messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); 
      } 
      //获取发送方号码 
      String address = messages[0].getOriginatingAddress(); 
      //获取短信内容 
      String fullMessage = ""; 
      for (SmsMessage message : messages) { 
        fullMessage += message.getMessageBody(); 
      } 
      sender.setText(address); 
      content.setText(fullMessage); 
    } 
  } 
  class SendStatusReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
      if (getResultCode() == RESULT_OK) { 
        //发送成功 
        Toast.makeText(context, "Send succeeded", Toast.LENGTH_LONG) 
            .show(); 
      } else { 
        //发送失败 
        Toast.makeText(context, "Send failed", Toast.LENGTH_LONG) 
            .show(); 
      } 
    } 
  } 
} 

activity_main:


<LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:orientation="vertical" > 
  <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="50dp" > 
    <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:padding="10dp" 
      android:text="From:" /> 
    <TextView 
      android:id="@+id/sender" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" /> 
  </LinearLayout> 
  <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="50dp" > 
    <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:padding="10dp" 
      android:text="Content:" /> 
    <TextView 
      android:id="@+id/content" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" /> 
  </LinearLayout> 
  <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="50dp" > 
    <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:padding="10dp" 
      android:text="To:" /> 
    <EditText 
      android:id="@+id/to" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_weight="1" /> 
  </LinearLayout> 
  <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="50dp" > 
    <EditText 
      android:id="@+id/msg_input" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_weight="1" /> 
    <Button 
      android:id="@+id/send" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:text="Send" /> 
  </LinearLayout> 
</LinearLayout> 

AndroidManifest:


<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  package="com.example.smstest" 
  android:versionCode="1" 
  android:versionName="1.0" > 
  <uses-sdk 
    android:minSdkVersion="14" 
    android:targetSdkVersion="17" /> 
  //接受短信 
  <uses-permission android:name="android.permission.RECEIVE_SMS" /> 
  //发送短信 
  <uses-permission android:name="android.permission.SEND_SMS" /> 
  <application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
      android:name="com.example.smstest.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
        <action android:name="android.intent.action.MAIN" /> 
        <cateGory android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
    </activity> 
  </application> 
</manifest> 
您可能感兴趣的文章:android中可以通过两种方式调用接口发送短信Android Mms之:短信发送流程(图文详解)Android实现将已发送的短信写入短信数据库的方法Android发送短信功能代码Android短信发送器实现方法Android编程实现读取手机联系人、拨号、发送短信及长按菜单操作方法实例小结Android实现短信发送功能Android实现短信加密功能(发送加密短信、解密本地短信)Android实现发送短信功能实例详解Android基础开发小案例之短信发送器


--结束END--

本文标题: Android接收和发送短信处理

本文链接: https://www.lsjlt.com/news/25839.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • PHP实现手机短信接口发送短信
    随着互联网的普及,短信已经成为人们日常生活中不可或缺的一部分。我们在各种场合下都会使用到短信,例如注册账号、确认订单、找回密码、验证码验证等等。其中,短信验证码验证是目前最常用的方式之一。而在实现短信验证码验证的过程中,短信发送接口的实现显...
    99+
    2023-05-22
    接口 PHP 手机短信
  • PHP调用乐信短信接口实现短信发送
    近年来,短信作为一种高效快捷的通信手段,被广泛地应用于各种场景,比如验证码验证、营销推广、订单通知等。而乐信短信平台作为国内领先的短信服务提供商,其稳定可靠的服务受到了广大客户的信任和好评。本文将介绍如何利用 PHP 编程语言,调用乐信短信...
    99+
    2023-05-21
    PHP 调用 短信接口
  • PHP对接阿里云短信接口实现发送短信
    随着互联网的普及和移动数据的发展,短信服务成为了传统营销和业务通知不可或缺的一部分。阿里云短信服务因为稳定、快速、高效、易于维护等优势,成为了众多企业和开发者的首选。在阿里云短信服务中,完整的调用过程包括:获取AK和SK、初始化客户端、设置...
    99+
    2023-05-21
    PHP 发送短信 阿里云短信接口
  • [Android]App之间发送和接收广播
    这里使用不同模块来实现app之间的广播通信,以两个app为例,通过点击按钮来实现的~   1. broad模块 - 忽略BootCompleteReceiver和MainActivity(这是其他的~)  步骤: (1)创建 BroadO...
    99+
    2023-09-18
    android java 开发语言
  • PHP调用鼎信通短信接口实现短信发送
    近年来,随着移动互联网的发展,短信成为了人们交流的一种重要工具。而如何实现短信的快速发送,对于企业来说则尤为重要。本文将介绍如何使用PHP语言调用鼎信通短信接口实现短信的自动发送。一、注册鼎信通账户并获取API接口密钥在使用鼎信通短信接口之...
    99+
    2023-05-21
    PHP 短信发送 鼎信通短信接口
  • PHP调用企信通短信接口实现短信发送
    企信通是目前国内最大的企业级短信服务提供商之一,其短信接口简单易用,为广大企业用户所喜爱。本文将介绍如何使用PHP调用企信通短信接口实现短信发送。一、申请企业短信接口账号首先,我们需要在企信通官网上申请企业短信接口账号。根据官网提示填写相关...
    99+
    2023-05-22
    PHP 短信发送 企信通
  • PHP调用腾讯云短信接口实现发送短信
    PHP调用腾讯云短信接口实现发送短信随着移动互联网的发展,短信成为了日常生活中必不可少的交流工具,而短信营销、验证码等应用也越来越普遍。腾讯云短信平台提供了一种便捷、安全、高效的短信服务,而PHP调用腾讯云短信接口实现发送短信也变得越来越简...
    99+
    2023-05-21
    PHP 腾讯云短信接口 发送短信
  • 怎么在Android中利用SmsManager发送短信
    本篇文章给大家分享的是有关怎么在Android中利用SmsManager发送短信,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。关键代码实现package com.ex...
    99+
    2023-05-30
    android smsmanager
  • 怎么在Android中调用发送的短信
    怎么在Android中调用发送的短信?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。功能:调用发送短信功能1 、 权限 <uses-permission an...
    99+
    2023-05-30
    android
  • PHP调用网易短信接口实现批量短信发送
    随着移动互联网的快速发展,短信营销成为越来越多企业所青睐的一种营销手段。而要想进行短信营销,首先需要解决的就是如何实现批量短信发送。本文将介绍如何使用PHP调用网易短信接口实现批量短信发送。申请网易短信接口首先需要申请网易短信接口,申请地址...
    99+
    2023-05-22
    PHP 批量短信发送 网易短信接口
  • PHP调用互亿无线短信接口实现短信发送
    互亿无线作为国内最具影响力的短信服务供应商之一,其提供的短信接口能够轻松实现短信发送,而且支持各种编程语言,包括PHP。下面将详细介绍如何使用PHP调用互亿无线短信接口实现短信发送。注册互亿无线账号并申请短信接口在使用互亿无线短信服务之前,...
    99+
    2023-05-23
    PHP 短信发送 互亿无线
  • python 发送和接收ActiveMQ
    ActiveMQ是java开发的消息中间件服务。可以支持多种协议(AMQP,MQTT,OpenWire,Stomp),默认的是OpenWire。而python与ActiveMQ的通信使用的是Stomp协议。而如果你的服务没有开启则需要配置...
    99+
    2023-01-31
    python ActiveMQ
  • PHP调用美联软通短信接口实现短信发送
    随着人们生活水平的提高和科技的发展,短信已成为人们交流的主要方式之一,越来越多的企业开始通过短信平台来实现营销、提醒等功能。在这个过程中,短信接口的选择显得尤为重要。本文将介绍如何通过PHP调用美联软通短信接口实现短信发送。一、美联软通短信...
    99+
    2023-05-21
    PHP 调用 短信发送
  • PHP调用聚合数据短信接口实现短信发送
    在现代化的互联网时代中,短信已成为人们生活中不可或缺的一部分,尤其是在移动互联网时代,短信的使用率日益提高。而短信接口的使用,更是成为各大企业和开发者必备的一环。那么,在使用PHP语言进行短信发送时,如何调用聚合数据短信接口呢?本篇文章将通...
    99+
    2023-05-21
    PHP 短信发送 聚合数据短信接口
  • PHP调用融云短信接口实现批量短信发送
    随着移动互联网时代的到来,短信已成为手机日常使用的重要方式之一。对于企业而言,短信发送是推广营销、客户维护和服务提醒的好方法,因此使用短信接口进行批量短信发送也成为常见的需求。融云是国内知名的即时通讯云服务提供商,提供了多种短信服务接口。本...
    99+
    2023-05-22
    PHP 批量短信发送 融云短信接口
  • PHP调用云之讯短信接口实现批量短信发送
    在现代社会,短信的应用越来越广泛,不仅仅用于个人之间的通讯,也被各行各业用于相关业务的通知、提醒等。如何实现批量短信发送成为很多企业、组织和个人必须面对的问题。而PHP调用云之讯短信接口则成为一种利用云端技术实现高效批量短信发送的方式。云之...
    99+
    2023-05-22
    PHP编程 短信发送 云之讯接口
  • PHP调用容联云通讯短信接口实现短信发送
    随着移动互联网的飞速发展,短信成为了人们生活中必不可少的一部分。无论是企业营销、快递通知、验证码验证,都需要用到短信功能。而容联云通讯短信接口作为一种有效的短信解决方案,被越来越多的企业所采用。本文将介绍如何使用PHP调用容联云通讯短信接口...
    99+
    2023-05-21
    PHP 短信发送 容联云通讯
  • STM32-串口通信(串口的接收和发送)
    文章目录 STM32的串口通信一、STM32里的串口通信二、串口的发送和接收串口发送串口接收 三、串口在STM32中的配置四、串口接收的两种实现方式1. 需要更改的地方2. 查询RXNE标...
    99+
    2023-10-12
    stm32 单片机 arm c语言 学习
  • PHP调用云片网短信接口实现批量短信发送
    随着移动互联网的发展,短信营销成为企业在互联网时代中推广的有效方式之一,但是短信的发送量惊人,如何实现短信的批量发送成为了一项技术难题,本文将对如何使用PHP调用云片网短信接口实现批量短信发送进行详细讲解。一、云片网短信接口介绍1、云片网简...
    99+
    2023-05-23
    PHP 短信接口 云片网
  • PHP调用梦网科技短信接口实现批量短信发送
    在当今信息化社会,短信成为人们生活中必不可缺的沟通方式之一。无论是企业还是个人,短信都成为了重要的互联网营销方式之一。作为开发者,如何使用PHP语言实现短信发送变得越来越重要和必不可少。本文将介绍如何使用PHP调用梦网科技短信接口实现批量短...
    99+
    2023-05-22
    PHP 短信接口 批量发送
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作