《Android------短信发射器》

//java
package com.fenghuo.sms;

import java.util.ArrayList;

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

@SuppressWarnings("deprecation")
public class MainActivity extends Activity {
    private EditText numberText;
    private EditText contentText;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        numberText = (EditText) this.findViewById(R.id.number);
        contentText =(EditText) this.findViewById(R.id.content);
        Button button = (Button) this.findViewById(R.id.button);
        button.setOnClickListener(new ButtononClickListener());
    }
    private final class ButtononClickListener implements View.OnClickListener{

		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, R.string.success, Toast.LENGTH_LONG).show();
		}
    }   
}
//main.xml
<?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/hello" />
    <EditText  
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/number"/>
    <TextView
        android:layout_width="fill_parent"
        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"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button"
        android:id="@+id/button"/>

</LinearLayout>
//string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">请输入手机号码</string>
    <string name="app_name">短信发送器</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"/>
《Android------短信发射器》_第1张图片 《Android------短信发射器》_第2张图片

你可能感兴趣的:(android)