andorid学习小案例--短信发送器

短信发送器

搭建完成android的开发环境以后,编写一个小的案例试一下,下面是我编写的android短信发送器。
构建完成的案列应该是这样的


右边可以输入手机号码,下面编辑短信内容,点击发送短信,会将短信内容成功发送至手机号码

1. 新建一个android工程,首先将界面写出来。考虑会在界面显示三段文字信息,在string.xml 中先定义好这三段信息

<string name="button">发送短信</string>
	<string name="phone_num">请输入手机号码</string>
	<string name="message_context">请输入短信内容</string>



2.观察案例的布局,可以采用一个线性布局嵌套一个相对布局来实现,即“输入手机号码” 与后面的EditText作为一个整体,放入一个相对布局中,再与下面的三个View组成线性布局
layout的xml文件如下所示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical" >
	<RelativeLayout 
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content">
    <TextView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="@string/phone_num" 
        android:id="@+id/numberlabel"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/phone"
        android:layout_toRightOf="@id/numberlabel"
        android:layout_marginTop="@id/numberlabel"
        android:layout_marginLeft="50dp"
        />
    </RelativeLayout>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/message_context" 
        />
	 <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       	android:minLines="3"
        android:id="@+id/meg_text"
        />
	 <Button
	     android:layout_width="wrap_content"
	     android:layout_height="wrap_content"
	     android:text="@string/button"
	     android:id="@+id/button"
	    />
	     
</LinearLayout>

3.对按钮“发送短信”添加响应
在onCreate方法里面,先获取 手机号码  短信内容  发送按钮的对象
phone_num = (EditText)this.findViewById(R.id.phone);
meg_text = (EditText)this.findViewById(R.id.meg_text);
Button send = (Button)this.findViewById(R.id.button);
因为phone_num 和meg_text在后面的方法中要使用到,所以将他们定义为全局变量
private EditText phone_num;
private EditText meg_text;
     
然后创建一个内部类来实现button的响应代码
private final class ButtonOnclickListener implements View.OnClickListener{
public void onClick(View arg0) {
String number = phone_num.getText().toString();
String meg = meg_text.getText().toString();
//短信管理器
SmsManager manager = SmsManager.getDefault();
//若是字数超过70个 分成几条发送
ArrayList <String> megs = manager.divideMessage(meg);
//for循环输出
for(String text:megs){
//参数说明  1 对方地址(电话号码) 2 短信中心(null表示默认) 3短信内容  4 发送状态(是否发送成功) 5对方是否收到
manager.sendTextMessage(number, null, text,null , null);
}
//Toast 通知                   参数 1 上下文    2 通知内容   3通知显示时间  
Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_SHORT).show();
}

}
这里 先将短信内容与短信号码得到为String类型。借助短信管理器SmsManager发送,因为短信字数只有70字,所以我们调用短信管理器的
divideMessage(String message)方法,如果有超过70字,则拆分成一个ArrayList的类型存放Message
发送则是靠调用manager的sendTextMessage()方法,该方法有五个参数1 对方地址(电话号码) 2 短信中心(null表示默认) 3短信内容  4 发送状态(是否发送成功) 5对方是否收到
最后在发送完成以后添加一个Toast通知
4.最后在onCreate方法里添加button的点击的监听
send.setOnClickListener(new ButtonOnclickListener());


完成的MainActivaty的代码如下

package com.example.message;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.telephony.SmsManager;
import android.view.*;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private EditText phone_num;
	private EditText meg_text;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		phone_num = (EditText)this.findViewById(R.id.phone);
		meg_text = (EditText)this.findViewById(R.id.meg_text);
		Button send = (Button)this.findViewById(R.id.button);
		send.setOnClickListener(new ButtonOnclickListener());
		
	}
	private final class ButtonOnclickListener implements View.OnClickListener{
		public void onClick(View arg0) {
		 String number = phone_num.getText().toString();
		 String meg = meg_text.getText().toString();
		//短信管理器
		 SmsManager manager = SmsManager.getDefault();
		 //若是字数超过70个 分成几条发送
		 ArrayList <String> megs = manager.divideMessage(meg);
		 //for循环输出
		 for(String text:megs){
			 //参数说明  1 对方地址(电话号码) 2 短信中心(null表示默认) 3短信内容  4 发送状态(是否发送成功) 5对方是否收到
			 manager.sendTextMessage(number, null, text,null , null);
		 }		
		 //Toast 通知                   参数 1 上下文    2 通知内容   3通知显示时间  
		 Toast.makeText(getApplicationContext(), R.string.success, 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;
	}

}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值