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 发送短信与接收短信的更多相关文章

  1. Android使用BroadCastRecevier广播实现接收短信,并利用Toast弹出显示内容

    在上一篇文章 Android简单实现BroadCastReceiver广播机制 中简单的实现了一个广播机制,这里利用BroadCarstRecevier实现一个接收短信并显示内容的案例,当然至于接收到 ...

  2. android 发送自定义广播以及接收自定义广播

    发送自定义广播程序: 布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...

  3. Android软件开发之发送短信与系统短信库解析

    今天我和同学们讨论一下Android平台下如何调用系统方法发送短信.接收短信.系统的短信库相关的问题.进入正题,我们先使用Eclipse工具模拟给自己的模拟器发送一条短信.在Eclipse下打开DDM ...

  4. android学习十四(android的接收短信)

    收发短信是每一个手机主要的操作,android手机当然也能够接收短信了. android系统提供了一系列的API,使得我们能够在自己的应用程序里接收和发送短信. 事实上接收短信主要是利用我们前面学过的 ...

  5. 基于Socket客户端局域网或广域网内共享同一短信猫收发短信的开发解决方案

    可使同一网络(局域网或广域网)内众多客户端,共享一个短信猫设备短信服务器进行短信收发,短信服务器具备对客户端的管理功能. 下面是某市建设银行采用本短信二次开发平台时实施的系统方案图: 在该方案中,考虑 ...

  6. Android之发送短信和接收验证码

      最近项目需求需要发送短信和接收验证码并将验证码显示在输入框中 以下是我的记录    前提---权限     <uses-permission android:name="andro ...

  7. Android 短信模块分析(四) MMS之短信的发送与接收

     MMS之短信的发送与接收分析: 一.信息发送: com.android.mms.data.WorkingMessage.java 类 send()函数: public void send() { . ...

  8. android 发送短信的两种方式,以及接收报告和发送报告

               android发送短信,以及接收报告和发送报告          android中发送短信其实有两种方式,这个和打电话类似,大家可以了解一下:    一.调起系统发短信功能    ...

  9. android基础---->发送和接收短信

    收发短信应该是每个手机最基本的功能之一了,即使是许多年前的老手机也都会具备这项功能,而Android 作为出色的智能手机操作系统,自然也少不了在这方面的支持.今天我们开始自己创建一个简单的发送和接收短 ...

随机推荐

  1. Mongo中更新总结

    mongo中的更新其实也可以当做添加来使用 mongo中跟新有几种方式 save.update.upsert 执行save的时候如果这个文档有_id这个参数,save 会调用 upsert,否则会调用 ...

  2. 使用SVN提示“工作副本已经锁定”的解决办法

    更新或者提交前执行一下clean up.如果在当前目录执行该命令后,仍然提示锁定,就到上一层目录再执行下...

  3. php--group by

    1. GROUP BY 是分组查询, 一般 GROUP BY 是和聚合函数配合使用 group by 有一个原则,就是 select 后面的所有列中,没有使用聚合函数的列,必须出现在 group by ...

  4. Asp.net MVC 版本简史

    http://www.dotnet-tricks.com/Tutorial/mvc/XWX7210713-A-brief-history-of-Asp.Net-MVC-framework.html A ...

  5. 20145211 《Java程序设计》课程总结——桃花流水窅然去

    每周读书笔记链接汇总 20145211 <Java程序设计>第1周学习总结--小荷才露尖尖角 20145211 <Java程序设计>第2周学习总结--桃花依旧笑春风 20145 ...

  6. (leetcode)Add Digits

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  7. ios copy/strong/weak..使用总结

    总结 关于属性的这些选项的学习,做一下总结: 所有的属性,都尽可能使用nonatomic,以提高效率,除非真的有必要考虑线程安全. NSString:通常都使用copy,以得到新的内存分配,而不只是原 ...

  8. Selenium2学习-007-WebUI自动化实战实例-005-解决 Firefox 版本不兼容:org.openqa.selenium.WebDriverException: Failed to connect to binary FirefoxBinary

    此文主要讲述 Java 运行 Selenium 脚本时,因 Friefox 浏览器版本与 selenium-server-standalone-x.xx.x.jar 不兼容引起的 org.openqa ...

  9. vs vim 插件

    需要在visual studio安装vim插件,由于在visual studio联机失败,只能手动下载安装. 转载自以下链接: http://blog.csdn.net/lingtianyulong/ ...

  10. $.ajax所犯的错误。success后面不执行

    $.ajax({ type: 'post', url: '../AshxHandler/HandlerAddPhoto.ashx', data: { clientPath: photoName }, ...