欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

详解Android短信的发送和广播接收实现短信的监听

程序员文章站 2024-03-01 20:25:16
本文介绍了android短信的发送和广播接收者实现短信的监听,要注意android清单中权限的设置以及广播的注册监听实现,废话不多说,代码如下: 以下就是 android...

本文介绍了android短信的发送和广播接收者实现短信的监听,要注意android清单中权限的设置以及广播的注册监听实现,废话不多说,代码如下:

以下就是 android清单的xml

androidmanifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.zyw.broadcastsendsms"
  android:versioncode="1"
  android:versionname="1.0" >

  <uses-sdk
    android:minsdkversion="8"
    android:targetsdkversion="15" />

  <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/apptheme" >
    <activity
      android:name=".sendsms"
      android:label="@string/title_activity_send_sms" >
      <intent-filter>
        <action android:name="android.intent.action.main" />

        <category android:name="android.intent.category.launcher" />
      </intent-filter>
      
    </activity>
    <receiver android:name=".smsbroadcastreceiver">
      <intent-filter android:priority="1000">
        <action android:name="android.provider.telephony.sms_received"/>
      </intent-filter>
    </receiver>
    
  </application>
  <uses-permission android:name="android.permission.send_sms"></uses-permission><!--添加权限-->
  <uses-permission android:name="android.permission.receive_sms"></uses-permission> 
  <uses-permission android:name="android.permission.read_sms"></uses-permission> 
</manifest>

发送短息的主界面mian.xml和实现activity    sendsms.java

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:padding="10sp" >

  <textview
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="手机号码" />

  <edittext
    android:id="@+id/number"
    android:numeric="integer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="请输入电话号码" />

  <textview
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="短信内容" />

  <edittext
    android:id="@+id/content"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="请输入短信内容"
    android:lines="3" />

  <textview
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
  </textview>

  <button
    android:id="@+id/btnsend"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:paddingtop="20sp"
    android:text="发送短信" 
    android:onclick="send"
    />
 

</linearlayout>

实现activity 
sendsms.java

package com.zyw.broadcastsendsms;

import java.util.arraylist;

import android.app.activity;
import android.os.bundle;
import android.telephony.smsmanager;
import android.view.view;
import android.widget.edittext;
import android.widget.toast;

public class sendsms extends activity{
  private edittext num;
  private edittext content;
  
  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);
    num=(edittext) findviewbyid(r.id.number);
    content=(edittext) findviewbyid(r.id.content);
  }
  
  public void send(view view ) {
    string strno=num.gettext().tostring();
    string strcontent=content.gettext().tostring();
    
    smsmanager smsmanager = smsmanager.getdefault();
    //如果字数超过5,需拆分成多条短信发送
    if (strcontent.length() > 5) {
      arraylist<string> msgs = smsmanager.dividemessage(strcontent);
      for (string msg : msgs) {
        smsmanager.sendtextmessage(strno, null, msg, null, null);            
      }
    } else {
      smsmanager.sendtextmessage(strno, null, strcontent, null, null);
    }
    num.settext("");
    content.settext("");
    
    toast.maketext(sendsms.this, "短信发送完成", toast.length_long).show();
  }
}

广播接收者实现短信的监听  smsbroadcastreceiver.java

package com.zyw.broadcastsendsms;

import java.text.simpledateformat;
import java.util.date;
import android.content.broadcastreceiver;
import android.content.context;
import android.content.intent;
import android.os.bundle;
import android.telephony.smsmessage;
import android.widget.toast;

public class smsbroadcastreceiver extends broadcastreceiver {
  public void onreceive(context context, intent intent) {
    smsmessage msg = null;
      bundle bundle = intent.getextras();
      if (bundle != null) {
        object[] pdusobj = (object[]) bundle.get("pdus");
        for (object p : pdusobj) {
          msg= smsmessage.createfrompdu((byte[]) p);
          
          string msgtxt =msg.getmessagebody();//得到消息的内容
          
          date date = new date(msg.gettimestampmillis());//时间
          simpledateformat format = new simpledateformat("yyyy-mm-dd hh:mm:ss");
          string receivetime = format.format(date);
          
          string sendernumber = msg.getoriginatingaddress();
        
        if (msgtxt.equals("testing!")) {
          toast.maketext(context, "success!", toast.length_long)
              .show();
          system.out.println("success!");
          return;
        } else {
          toast.maketext(context, msgtxt, toast.length_long).show();
          system.out.println("发送人:"+sendernumber+" 短信内容:"+msgtxt+"接受时间:"+receivetime);
          return;
        }
      }
      return;
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。