android开发第一弹:短信SMS

来源:互联网 发布:h3c查看端口vlan 编辑:程序博客网 时间:2024/05/16 05:35

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    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="com.example.sendsms.MainActivity">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:orientation="horizontal">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="号\u3000码:"            android:textSize="22sp"/>        <EditText            android:id="@+id/et_number"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:hint="请输入号码"/>    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:orientation="horizontal">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="内\u3000容:"            android:textSize="22sp"/>        <EditText            android:id="@+id/et_content"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:hint="请输入内容"/>    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="0.7"        android:orientation="horizontal">        <Button            android:id="@+id/bt_send"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:text="发\u3000送"            android:textSize="22sp"            android:background="@drawable/border"/>    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="4"        android:orientation="horizontal">        <LinearLayout            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1">            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="没有号码?"                android:textSize="22sp"                android:background="@null"                android:layout_gravity="center"                android:layout_marginLeft="30dp"                android:layout_marginTop="40dp"/>        </LinearLayout>        <LinearLayout            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1">            <Button                android:id="@+id/bt_directsend"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="直接发送"                android:textSize="22sp"                android:layout_gravity="center"                android:layout_marginLeft="30dp"                android:layout_marginTop="40dp"                android:background="@drawable/bg_round_rectangle"/>        </LinearLayout>    </LinearLayout></LinearLayout>

MainActivity.java

package com.example.sendsms;import android.app.Activity;import android.os.Bundle;import android.telephony.SmsManager;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import java.util.ArrayList;public class MainActivity extends Activity {    //声明变量    EditText et_number,et_content;    Button bt_send,bt_directsend;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //绑定布局        setContentView(R.layout.activity_main);        //初始化        init();    }    //实例化变量    private void init() {        et_number = (EditText) findViewById(R.id.et_number);        et_content = (EditText) findViewById(R.id.et_content);        bt_send = (Button) findViewById(R.id.bt_send);        bt_directsend = (Button) findViewById(R.id.bt_directsend);        //给bt_send按钮设置点击监听器        bt_send.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //获得手机号码                String number = et_number.getText().toString().trim();                //获得短信内容                String content = et_content.getText().toString().trim();                if (number.equals("")){                    Toast.makeText(MainActivity.this,"请输入手机号码",Toast.LENGTH_SHORT).show();                    return;                }                //获得短信消息管理器                SmsManager smsManager = SmsManager.getDefault();                //判断内容是否大于60个字                if (content.length()>60){                    //分割内容                    ArrayList<String> msgs = smsManager.divideMessage(content);                for (String msg:msgs){                    //发送短信                    smsManager.sendTextMessage(number,null,msg,null,null);                }                }else {                    //发送内容                    smsManager.sendTextMessage(number,null,content,null,null);                }                Toast.makeText(MainActivity.this,"发送完成",Toast.LENGTH_SHORT).show();            }        });        //给bt_directsend设置点击监听器        bt_directsend.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //获得手机号码                String number = et_number.getText().toString().trim();                //获得短信内容                String content = et_content.getText().toString().trim();                if (number.equals("")){                    Toast.makeText(MainActivity.this,"请输入手机号码",Toast.LENGTH_SHORT).show();                    return;                }                //获得短信消息管理器                SmsManager smsManager = SmsManager.getDefault();                //判断内容是否大于60个字                if (content.length()>60){                    //分割内容                    ArrayList<String> msgs = smsManager.divideMessage(content);                    for (String msg:msgs){                        //发送短信                        smsManager.sendTextMessage(number,null,msg,null,null);                    }                }else {                    //发送内容                    smsManager.sendTextMessage(number,null,content,null,null);                }                Toast.makeText(MainActivity.this,"发送完成",Toast.LENGTH_SHORT).show();            }        });    }}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sendsms">    //注册发送短信的权限    <uses-permission android:name="android.permission.SEND_SMS"/>
    <application android:allowBackup="true" android:icon="@drawable/type_big_5"        android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

0 0
原创粉丝点击