Android 发送短信与接收短信
package com.example.testsms; 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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity {
private Button send;
private TextView number;
private TextView content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send = (Button) this.findViewById(R.id.button1);
number = (TextView) this.findViewById(R.id.textView2);
content = (TextView) this.findViewById(R.id.textView1);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com");
MyReceiver myReceiver = new MyReceiver();
registerReceiver(myReceiver,intentFilter);
send.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SmsManager smsManager = SmsManager.getDefault();
Intent sentIntent = new Intent("com");
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, sentIntent, 0);
smsManager.sendTextMessage((String)number.getText(),null,(String)content.getText(),pendingIntent,null);
}
}); }
class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
if(getResultCode()==RESULT_OK){
Toast.makeText(MainActivity.this,"success",1).show();
}
}
}
}
package com.example.mysms6; import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.app.Activity;
import android.telephony.SmsMessage;
import android.view.Menu;
import android.widget.TextView; public class MainActivity extends Activity {
private TextView textView1;
private TextView textView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) this.findViewById(R.id.textView);
textView2 = (TextView) this.findViewById(R.id.textView3);
IntentFilter intentFilter = new IntentFilter();
intentFilter.setPriority(800);
intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
MyReceiver myReceiver = new MyReceiver();
registerReceiver(myReceiver,intentFilter);
}
class MyReceiver extends BroadcastReceiver{ @Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
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();
}
textView1.setText(address);
textView2.setText(fullMessage);
abortBroadcast();
}
}
}
方便自己以后查阅吧 一个用eclipse 一个用Android Studio
Android 发送短信与接收短信的更多相关文章
- Android使用BroadCastRecevier广播实现接收短信,并利用Toast弹出显示内容
在上一篇文章 Android简单实现BroadCastReceiver广播机制 中简单的实现了一个广播机制,这里利用BroadCarstRecevier实现一个接收短信并显示内容的案例,当然至于接收到 ...
- android 发送自定义广播以及接收自定义广播
发送自定义广播程序: 布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...
- Android软件开发之发送短信与系统短信库解析
今天我和同学们讨论一下Android平台下如何调用系统方法发送短信.接收短信.系统的短信库相关的问题.进入正题,我们先使用Eclipse工具模拟给自己的模拟器发送一条短信.在Eclipse下打开DDM ...
- android学习十四(android的接收短信)
收发短信是每一个手机主要的操作,android手机当然也能够接收短信了. android系统提供了一系列的API,使得我们能够在自己的应用程序里接收和发送短信. 事实上接收短信主要是利用我们前面学过的 ...
- 基于Socket客户端局域网或广域网内共享同一短信猫收发短信的开发解决方案
可使同一网络(局域网或广域网)内众多客户端,共享一个短信猫设备短信服务器进行短信收发,短信服务器具备对客户端的管理功能. 下面是某市建设银行采用本短信二次开发平台时实施的系统方案图: 在该方案中,考虑 ...
- Android之发送短信和接收验证码
最近项目需求需要发送短信和接收验证码并将验证码显示在输入框中 以下是我的记录 前提---权限 <uses-permission android:name="andro ...
- Android 短信模块分析(四) MMS之短信的发送与接收
MMS之短信的发送与接收分析: 一.信息发送: com.android.mms.data.WorkingMessage.java 类 send()函数: public void send() { . ...
- android 发送短信的两种方式,以及接收报告和发送报告
android发送短信,以及接收报告和发送报告 android中发送短信其实有两种方式,这个和打电话类似,大家可以了解一下: 一.调起系统发短信功能 ...
- android基础---->发送和接收短信
收发短信应该是每个手机最基本的功能之一了,即使是许多年前的老手机也都会具备这项功能,而Android 作为出色的智能手机操作系统,自然也少不了在这方面的支持.今天我们开始自己创建一个简单的发送和接收短 ...
随机推荐
- iOS自定义控件开发详解
http://blog.csdn.net/zhangao0086/article/details/45622875
- Oozie协作框架
一:概述 1.大数据协作框架 2.Hadoop的任务调度 3.Oozie的三大功能 Oozie Workflow jobs Oozie Coordinator jobs Oozie Bundle 4. ...
- Linux Socket过程详细解释(包括三次握手建立连接,四次握手断开连接)
我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览器浏览网页时,浏览器的进程怎么与web 服务器通信的?当你用QQ聊天时,QQ进程怎么与服务器或你好友所在的QQ进程通信?这些都得靠s ...
- dom classList
才发现dom对象就有classList属性,通过它可以判断该dom是否有指定的class名存在. var tar = e.target; var classList = tar.classList; ...
- DMV to track the temp file usage for SQLServer
There are three DMVs you can use to track tempdb usage: sys.dm_db_task_space_usagesys.dm_db_session_ ...
- myeclipse中的web项目导入到eclipse中注意事项,项目部署到tomcat后无法访问jsp文件
打开eclipse,点击空白处,右键可以看到import>general>existing projects into workspace>next>选择你的myeclipse ...
- 构造方法后面加上了:base
今天看公司软件的代码碰到一个奇怪的方法 ,寻早了各种方法后终于明白了,在构造方法后面加上了:base(message),该类如下: public NONEDIException(string mess ...
- C# GridControl 行背景颜色
使用C# DevExpress_gridControl 行号行样式显示行背景颜色,必须取消自动变换行色属性 取消Focus变色,属性为OptionSelection--EnableAppearance ...
- Json.Net Demo2
新建一个空的Web项目,名称JsonServer,该网页实现Ajax数据请求和响应. 添加Newtonsoft.Json.dll的Dll引用,添加JQuery API文件,目录结构如下: 新建一个Pe ...
- MFC对话框Dialog控件处理程序handler因为public修饰符导致无法访问
比如说你的Dialog有一个Button名为Confirm,对应IDC_CONFIRM,处理程序handler为OnConfirm 那么OnConfirm必须是protected属性,如果是publi ...