1. 界面布局

 

    界面代码:

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/number" />

    <EditText 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@null" 
        android:id="@+id/number2"
        />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/content" />

    <EditText 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minLines="3"
        android:id="@+id/content"
        android:hint="@null" 
        />
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button"
        android:id="@+id/button"
        />
</LinearLayout>

 

2.主代码

package com.example.sms;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    EditText numberText;
    EditText contentText;
    Button button;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        numberText = (EditText)this.findViewById(R.id.number2);
        contentText = (EditText)this.findViewById(R.id.content);
        button = (Button)this.findViewById(R.id.button);
        
        button.setOnClickListener(new ButtonClickListener());    
        
    }
    
    private final class ButtonClickListener implements View.OnClickListener{

        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {
            // TODO 自动生成的方法存根
            String number = numberText.getText().toString();
            String content = contentText.getText().toString();
            SmsManager manager = SmsManager.getDefault();
            
            //如果短信过长,可以自动拆分,如果字数超过70,需拆分成多条短信发送            
            ArrayList<String> texts = manager.divideMessage(content);
            for(String text : texts)
            {
                //发送短信指令
                manager.sendTextMessage(number, null, text, null, null);
                //最后第二个参数为短信已发送的广播意图,最后一个参数为短信对方已收到短信的广播意图
            }
            
            //显示发送成功
            Toast.makeText(MainActivity.this, R.string.success, Toast.LENGTH_LONG).show();
        }
    }
}

 

3.string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">sms</string>
    <string name="hello_world">Hello world!</string>
    <string name="number">请输入手机号:</string>
    <string name="content">请输入内容:</string>
    <string name="button">发送</string>
    <string name="success">发送成功</string>

</resources>

 

4.申请权限(发短信)

 

 

 

注意:软件点发送按钮后,有三种提示方式:

posted on 2015-09-11 23:23  努力建设我的管道!  阅读(325)  评论(0编辑  收藏  举报