本文介绍了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); } } else { smsManager.sendTextMessage(strNo,strContent,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; } }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的详解Android短信的发送和广播接收实现短信的监听全部内容,希望文章能够帮你解决详解Android短信的发送和广播接收实现短信的监听所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)