Android基础学习 - 发送短信:

 
 

发送短信。注意SendSMS加权限。AndroidManifest.xml =》 Permissions =》 add

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:layout_marginLeft="36dp"
        android:layout_marginTop="32dp"
        android:text="@string/phoneNumber"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ff2800" />

    <EditText
        android:lines="1"
        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="38dp"
        android:ems="11"
        android:inputType="phone" >

        <requestFocus />
    </EditText>

    <EditText
        android:lines="3"
        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="22dp"
        android:ems="10"
        android:inputType="textMultiLine" />

    <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="33dp"
        android:text="@string/please_neirong"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ff2800" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText2"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="27dp"
        android:text="@string/send" />

</RelativeLayout>



MainActivity.java

package com.ichano.androidsms;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.telephony.SmsManager;
import android.text.TextUtils;
import android.view.Menu;
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 implements OnClickListener{
	
	private EditText etNumber;
	private EditText etContent;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取到手机号和密码
        etNumber = (EditText) findViewById(R.id.editText1);
        etContent = (EditText)findViewById(R.id.editText2);
        //获取到发送Button的点击事件
        Button btSend = (Button) findViewById(R.id.button1);
        btSend.setOnClickListener(this);
    }
    
    public void onClick(View v){
    	switch(v.getId()){
    		case R.id.button1:
    			//获取框内电话号码
    			String number = etNumber.getText().toString();
    			//获取框内短信内容
    			String content = etContent.getText().toString();
    			
    			if (TextUtils.isEmpty(content)||TextUtils.isEmpty(number)) {
    				Toast.makeText(this, "电话号码或者内容不能为空", Toast.LENGTH_SHORT).show(); 				
    				return;
    			}
    			else {
    				//短信管理器
    				SmsManager smsManager = SmsManager.getDefault();
    				ArrayList<String> contents = smsManager.divideMessage(content);
    				//类似于iOS中 for in 遍历
    				for (String str : contents) {
    					smsManager.sendTextMessage(number, null, str, null, null);
    				}
    			}
    		break;
    	}
    	
    }
  
}


你可能感兴趣的:(android,遍历,switch,手机,短信)