Android接收和发送短信处理

 更新时间:2016年01月15日 10:37:10   作者:xu佳佳  
这篇文章主要介绍了Android接收和发送短信处理的相关资料,具有一定的参考价值,需要的朋友可以参考下

关于短信接收处理方面,当前已经有一些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实现京东秒杀界面

    这篇文章主要为大家详细介绍了Android实现京东秒杀界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-04-04
  • Android 中基于TabLayout+ViewPager实现标签卡效果

    Android 中基于TabLayout+ViewPager实现标签卡效果

    这篇文章主要介绍了Android 中基于TabLayout+ViewPager实现标签卡效果,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-12-12
  • 使用Jetpack Compose实现翻转卡片效果流程详解

    使用Jetpack Compose实现翻转卡片效果流程详解

    Jetpack Compose 是一款基于 Kotlin 的声明式 UI 工具包,可以方便地创建漂亮的用户界面。使用 Compose 的动画 API 和可绘制 API,可以轻松实现翻转卡片效果。通过设置旋转角度和透明度等属性,可以使卡片沿着 Y 轴翻转,并实现翻页效果
    2023-05-05
  • Android利用ViewDragHelper轻松实现拼图游戏的示例

    Android利用ViewDragHelper轻松实现拼图游戏的示例

    本篇文章主要介绍了Android利用ViewDragHelper轻松实现拼图游戏的示例,非常具有实用价值,需要的朋友可以参考下
    2017-11-11
  • Android Studio使用自定义对话框效果

    Android Studio使用自定义对话框效果

    这篇文章主要为大家详细介绍了Android Studio使用自定义对话框效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • Kotlin对象的懒加载方式by lazy 与 lateinit 异同详解

    Kotlin对象的懒加载方式by lazy 与 lateinit 异同详解

    这篇文章主要为大家介绍了Kotlin对象的懒加载方式by lazy 与 lateinit 异同详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • android第三方分享方式的简单实现

    android第三方分享方式的简单实现

    这篇文章主要为大家详细介绍了android第三方分享方式的简单实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • Android自定义控件ListView下拉刷新的代码

    Android自定义控件ListView下拉刷新的代码

    今天小编就为大家分享一篇关于Android自定义控件ListView下拉刷新的代码,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • Android实现图片文字轮播特效

    Android实现图片文字轮播特效

    这篇文章主要介绍了Android图片文字轮播效果,分别实现图片和文字自动滚动,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-01-01
  • Android中实现WebView和JavaScript的互相调用详解

    Android中实现WebView和JavaScript的互相调用详解

    这篇文章主要给大家介绍了关于Android中实现WebView和JavaScript的互相调用的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友下面来一起看看吧。
    2018-03-03

最新评论