Android实例-发送信息

(1)项目结构:

Android实例-发送信息_第1张图片

package com.example.messagesender;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.telephony.gsm.SmsManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	private EditText numberText; // 手机号
	private EditText contentText;// 内容
	private Button button1;// 发送按钮

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		numberText = (EditText) this.findViewById(R.id.editText1);
		contentText = (EditText) this.findViewById(R.id.editText2);
		button1 = (Button) this.findViewById(R.id.button1);
		button1.setOnClickListener(new ButtonClickListener());
	}

	private final class ButtonClickListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			String number = numberText.getText().toString();
			String content = contentText.getText().toString();
			// 获得短信发送管理对象
			SmsManager manager = SmsManager.getDefault();

			// 对字数超过的时候,拆分短信
			ArrayList<String> texts = manager.divideMessage(content);

			// 迭代发送短信
			for (String text : texts) {
				manager.sendTextMessage(number, null, text, null, null);
			}
			Toast.makeText(MainActivity.this, "发送成功!", 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;
	}

}

activity_main.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"
    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=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="请输入手机号" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="20dp"
        android:ems="10" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="18dp"
        android:text="请输入短信内容" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="25dp"
        android:ems="10"
        android:minLines="3" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="18dp"
        android:text="发送信息" />

</RelativeLayout>

Android实例-发送信息_第2张图片

你可能感兴趣的:(Android实例-发送信息)