Android中简单的发短信应用

MessageSend.java

package com.ststudy.Message;

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 java.util.ArrayList;

/**
 * Created by aaron on 9/1/15.
 */
public class MessageSend extends Activity {

    private EditText mEtPhoneNumber = null;
    private EditText mEtMessage = null;
    private Button mBtSend = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sendMessage();
    }


    //发送短信
    public void sendMessage()
    {
        //获取ID
        mEtPhoneNumber = (EditText) findViewById(R.id.etPhoneNumber);
        mEtMessage = (EditText) findViewById(R.id.etMessage);
        mBtSend = (Button) findViewById(R.id.btSend);


        //按钮点击
        mBtSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String _phoneNumber = mEtPhoneNumber.getText().toString();
                String _content = mEtMessage.getText().toString();
                SmsManager sms = SmsManager.getDefault();

                ArrayList smss = sms.divideMessage(_content);

                for (String string : smss) {
                    sms.sendTextMessage(_phoneNumber, null, string, null, null);
                }
            }
        });
    }
}

activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <EditText
            android:id="@+id/etPhoneNumber"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入手机号"
            android:inputType="phone"/>


    <EditText
            android:id="@+id/etMessage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:lines="5"
            android:gravity="left"
            android:hint="请输入短信内容"/>

    <Button
            android:id="@+id/btSend"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="发送短信"/>

LinearLayout>

AndroidManifest.xml


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.ststudy.Message"
          android:versionCode="1"
          android:versionName="1.0">

    <uses-sdk android:minSdkVersion="13"/>
    <application android:label="@string/app_name">
        <activity android:name=".MessageSend">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            intent-filter>
        activity>
    application>


    
    <uses-permission android:name="android.permission.SEND_SMS"/>
manifest>

运行结果:
Android中简单的发短信应用_第1张图片
Android中简单的发短信应用_第2张图片
Android中简单的发短信应用_第3张图片

你可能感兴趣的:(Android之路。。。)