短信发送器的实现

来源:互联网 发布:全民突击刷枪内购软件 编辑:程序博客网 时间:2024/05/20 01:47

1.新建名为sendsms项目
2.Activity_sms.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"    tools:context="${relativePackage}.${activityClass}" >    <TextView        android:id="@+id/tvphone"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/tip_phone" />    <EditText        android:id="@+id/etphone"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/tvphone"        android:inputType="phone" />    <TextView        android:id="@+id/tvsms"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/etphone"        android:text="@string/tip_sms" />    <!-- 作业思考 怎么实现自动换行的效果 -->    <EditText        android:id="@+id/etsms"        android:layout_width="match_parent"        android:layout_height="100dp"        android:gravity="top"        android:layout_below="@+id/tvsms"        android:inputType="textMultiLine" />    <Button        android:id="@+id/sendBtn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/etsms"        android:text="@string/tip_send" /></RelativeLayout>字符串资源:<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">sendsms</string>    <string name="hello_world">Hello world!</string>    <!-- 准备字符串的资源 -->    <string name="tip_phone">请输入电话号码</string>    <string name="tip_sms">请输入发送的信息</string>    <string name="tip_send">发送</string></resources>

3.SmsActivity.java

package com.example.sendsms;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.telephony.SmsManager;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class SmsActivity extends Activity {    private Button sendBtn;    private EditText etphone,etsms;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // 设置显示的视图        setContentView(R.layout.activity_sms);        // 获取点击发送按钮的控件对象        sendBtn = (Button) findViewById(R.id.sendBtn);        //获取电话号码的控件对象        etphone=(EditText) findViewById(R.id.etphone);        //获取要发送信息的控件对象        etsms = (EditText) findViewById(R.id.etsms);        // 注册按钮的点击事件        sendBtn.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                   //1.获取电话号码及发送的短信内容                String phone = etphone.getText().toString();                String content = etsms.getText().toString();                //2.发短信的管理器对象                SmsManager smsManager = SmsManager.getDefault();                //3.拆分短信内容                List<String> list = smsManager.divideMessage(content);                //4.遍历发送信息                for(String sms:list){                    //5.逐条发送短信                    smsManager.sendTextMessage(phone, null, sms, null, null);                }                //5.提示短信发送成功                Toast.makeText(SmsActivity.this, "发短信成功", Toast.LENGTH_LONG).show();            }        });    }}/** * 1.添加发送短信的权限 * 2.获取发送短信管理器对象 * 3.执行发送操作(自动拆分70字符 160) *  */

4.在AndroidManifest.xml中添加发短信的权限

  <!-- 发送短信的权限 -->    <uses-permission android:name="android.permission.SEND_SMS"/>
4 0
原创粉丝点击