andriod发送短信小程序

源程序:

package com.zizhu.sms;

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 MainActivity extends Activity {
	
	private EditText number = null;
	private EditText content = null;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        number = (EditText)findViewById(R.id.number);
        content = (EditText)findViewById(R.id.content);
        Button sendBtn = (Button)findViewById(R.id.button);
        sendBtn.setOnClickListener(new ButtonClickListener());
    }
	
	private final class ButtonClickListener implements OnClickListener{

		public void onClick(View view) {
			SmsManager manager = SmsManager.getDefault();
			//当短信内容查过70个字符的时候,需要拆分
			List<String> contents = manager.divideMessage(content.getText().toString());
			for(String con : contents){
				manager.sendTextMessage(number.getText().toString(), null, con, null, null);
			}
			Toast.makeText(MainActivity.this, R.string.success, Toast.LENGTH_LONG).show();
		}
		
	}
}

//布局文件

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/number"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

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

    <EditText
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minLines="3" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button" />

</LinearLayout>


//界面相应的字符串

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

    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">Sms</string>
    <string name="number">请输入手机号码</string>
    <string name="content">请输入短信内容</string>
    <string name="button">发送</string>
    <string name="success">发送成功</string>

</resources>

//最后需要提供相应的权限

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


你可能感兴趣的:(andriod发送短信小程序)