当前位置: 首页 > news >正文

Android实例-发送信息

(1)项目结构:


package com.example.messagesender;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.telephony.gsm.SmsManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	private EditText numberText; // 手机号
	private EditText contentText;// 内容
	private Button button1;// 发送按钮

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		numberText = (EditText) this.findViewById(R.id.editText1);
		contentText = (EditText) this.findViewById(R.id.editText2);
		button1 = (Button) this.findViewById(R.id.button1);
		button1.setOnClickListener(new ButtonClickListener());
	}

	private final class ButtonClickListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			String number = numberText.getText().toString();
			String content = contentText.getText().toString();
			// 获得短信发送管理对象
			SmsManager manager = SmsManager.getDefault();

			// 对字数超过的时候,拆分短信
			ArrayList<String> texts = manager.divideMessage(content);

			// 迭代发送短信
			for (String text : texts) {
				manager.sendTextMessage(number, null, text, null, null);
			}
			Toast.makeText(MainActivity.this, "发送成功!", Toast.LENGTH_SHORT)
					.show();

		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="请输入手机号" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="20dp"
        android:ems="10" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="18dp"
        android:text="请输入短信内容" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="25dp"
        android:ems="10"
        android:minLines="3" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="18dp"
        android:text="发送信息" />

</RelativeLayout>


相关文章:

  • 利用jQuery实现鼠标滑过整行变色
  • Android项目之无线点餐(1)--点餐系统数据库设计
  • HDU 4757 Tree 可持久化字典树
  • Android项目之无线点餐(2)--用户登录的客户端和服务器端实现
  • 千变万化的ViewPager切换动画(1)--仅支持3.0以上版本的官方方法
  • Canopy聚类算法与Mahout中的实现
  • Android基础学习—下载并在Eclipse中关联Android源码
  • 【html】【11】函数名称约束规范
  • 千变万化的ViewPager切换动画(2)--自定义ViewPager的实现方法
  • 二叉树习题之重建二叉树
  • WebView的简单入门
  • 持续集成
  • Android开发Eclipse连接真机
  • 吐槽贴-微信公众号那些让人想起神兽的坑
  • No orientation specified, and the default is horizontal.
  • 【402天】跃迁之路——程序员高效学习方法论探索系列(实验阶段159-2018.03.14)...
  • Create React App 使用
  • ES6系列(二)变量的解构赋值
  • export和import的用法总结
  • Java比较器对数组,集合排序
  • java小心机(3)| 浅析finalize()
  • Java知识点总结(JavaIO-打印流)
  • js中forEach回调同异步问题
  • Linux各目录及每个目录的详细介绍
  • oldjun 检测网站的经验
  • python docx文档转html页面
  • React系列之 Redux 架构模式
  • 阿里研究院入选中国企业智库系统影响力榜
  • 前端面试总结(at, md)
  • 浅谈JavaScript的面向对象和它的封装、继承、多态
  • 用jquery写贪吃蛇
  • NLPIR智能语义技术让大数据挖掘更简单
  • 长三角G60科创走廊智能驾驶产业联盟揭牌成立,近80家企业助力智能驾驶行业发展 ...
  • ​第20课 在Android Native开发中加入新的C++类
  • #pragam once 和 #ifndef 预编译头
  • #宝哥教你#查看jquery绑定的事件函数
  • %3cscript放入php,跟bWAPP学WEB安全(PHP代码)--XSS跨站脚本攻击
  • (+3)1.3敏捷宣言与敏捷过程的特点
  • (AtCoder Beginner Contest 340) -- F - S = 1 -- 题解
  • (C)一些题4
  • (Java)【深基9.例1】选举学生会
  • (TOJ2804)Even? Odd?
  • (超简单)构建高可用网络应用:使用Nginx进行负载均衡与健康检查
  • (附源码)php新闻发布平台 毕业设计 141646
  • (一)VirtualBox安装增强功能
  • (转)详解PHP处理密码的几种方式
  • (轉貼) 資訊相關科系畢業的學生,未來會是什麼樣子?(Misc)
  • .MSSQLSERVER 导入导出 命令集--堪称经典,值得借鉴!
  • .NET Core 实现 Redis 批量查询指定格式的Key
  • .NET 动态调用WebService + WSE + UsernameToken
  • .NET 简介:跨平台、开源、高性能的开发平台
  • .NET企业级应用架构设计系列之应用服务器
  • [AX]AX2012 R2 出差申请和支出报告
  • [boost]使用boost::function和boost::bind产生的down机一例
  • [C#]winform使用引导APSF和梯度自适应卷积增强夜间雾图像的可见性算法实现夜间雾霾图像的可见度增强