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>

时间: 2024-07-28 19:31:32

Android实例-发送信息的相关文章

Android Socket发送信息时闪退

尝试Android写Socket通信的时候,遇到的个坑,记录一下: 1.无法建立连接. 原因:没有添加网络使用权限请求: 解决方式:在"AndroidMainfest.xml"中添加<uses-permission android:name="android.permission.INTERNET"/> 2.建立连接后正常接收信息,但发送信息时直接闪退,LogCat中异常提示为:android.os.NetworkOnMainThreadExceptio

Android实例-路径信息及文件和文件夹的操作(XE8+小米2)

unit Unit1; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.ScrollBox, FMX.Memo, FMX.Controls.Presentation, FMX.StdCtrls; type TForm1 =

Android实例-路径信息及文件和文件夹的操作

1 unit Unit1; 2 3 interface 4 5 uses 6 System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, 7 FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.ScrollBox, 8 FMX.Memo, FMX.Controls.Presentation, FMX.StdCtrls

【小功能2】android获取手机信息(号码,内存,CPU,分辨率,MAC,IP,SD卡,IMEI,经纬度,信号强度等等)

为了实现一个功能,需要搜集手机信息,自己先在网上找了相关信息进行了汇总,主要是汇集手机的信息,一般想要的信息在手机设置->关于手机->状态消息里面包含了手机的各种信息,下面的代码中也主要显示了那些信息,但是源码的方法我还没有看,先把总结的贴出来.先上图(太多就截取几个).  上代码啦,太多了,就写主要代码了. // 获取Android手机中SD卡存储信息 获取剩余空间 public void getSDCardInfo() { // 在manifest.xml文件中要添加 /* * <u

android开发异常信息收集程序代码

首先创建全局的Application ,此Application全局通用. package com.demo.utils; import com.demo.exception.CrashHandler; import android.app.Application; /** * 全局的context,任意位置调用 * @author Administrator * */ public class GlobalApplication extends Application { private st

Android实例-Delphi开发蓝牙官方实例解析(XE10+小米2+小米5)

Android实例-Delphi开发蓝牙官方实例解析(XE10+小米2+小米5) 相关资料:1.http://blog.csdn.net/laorenshen/article/details/411498032.http://www.cnblogs.com/findumars/p/5149128.html 一.理清概念1.蓝牙设备:是指代有蓝牙通信的手机.电脑.平板.打印机.耳机等.2.设备名称:是指设备打开蓝牙功能后,在其他设备中显示的名字,如图1用的A.B.C等.3.蓝牙关态:如果A手机没有

Android Mms 接收信息流程

信息的接收工作是由底层来完成的,当有一个 新的信息时底层完成接收后会以Intent的方式来通知上层应用,信息的相关内容也包含在Intent当中,Android所支持的信息Intent都定 义在android.provider.Telephony.Intents里面. 短信的接收 短信接收,对于上层应用程序来讲就是要处理广播事件SMS_RECEIVED_ACTION,它是由Frameworks发出告诉上层有新的SMS已收 到.在Mms中,是由PrivilegedSmsReceiver来处理,它收到

分享45个android实例源码,很好很强大

分享45个android实例源码,很好很强大http://www.apkbus.com/android-20978-1-1.html andriod闹钟源代码http://www.apkbus.com/android-20974-1-1.html android源码分享之指南针程序http://www.apkbus.com/android-20973-1-1.html 重力感应的测试程序andriod源代码http://www.apkbus.com/android-20972-1-1.html

分享45个android实例源码,很好很强大.收藏吧!!!

andriod闹钟源代码http://www.apkbus.com/android-20974-1-1.htmlandroid源码分享之指南针程序http://www.apkbus.com/android-20973-1-1.html重力感应的测试程序andriod源代码http://www.apkbus.com/android-20972-1-1.htmlandroid源码分享之时光日志个人日程管理http://www.apkbus.com/android-20969-1-1.htmlOpen