SMS短信发送

直接看例子:

main.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
    Android:orientation="vertical"  
    Android:layout_width="fill_parent"  
    Android:layout_height="fill_parent"  
    >  
<TextView   
    Android:text="@string/inputMobile"   
    Android:layout_width="fill_parent"   
    Android:layout_height="wrap_content">  
</TextView>  
<EditText   
    Android:id="@+id/mobile"   
    Android:layout_width="fill_parent"   
    Android:layout_height="wrap_content">  
</EditText>  
<TextView   
    Android:text="@string/inputMessage"  
    Android:layout_width="fill_parent"   
    Android:layout_height="wrap_content">  
</TextView>  
<EditText   
    Android:id="@+id/Message"   
    Android:layout_width="fill_parent"   
    Android:layout_height="wrap_content">  
</EditText>  
<Button   
    Android:text="@string/Submit"   
    Android:id="@+id/Submit"   
    Android:layout_width="wrap_content"   
    Android:layout_height="wrap_content">  
</Button>  
</LinearLayout>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string name="app_name">短信发送器</string>  
    <string name="inputMobile">请输入手机号:</string>  
    <string name="inputMessage">请输入短信内容:</string>  
    <string name="Submit">发送短信</string>  

</resources>

SmsautoActivity.java

package com.auto;

import java.util.List;

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

public class SmsautoActivity extends Activity {
	/** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
          
        final EditText mobileNoEditText = (EditText) findViewById(R.id.mobile);  
        final EditText messageEditText = (EditText) findViewById(R.id.Message);  
        Button submitButton =  (Button) findViewById(R.id.Submit);  
          
        submitButton.setOnClickListener(new View.OnClickListener() {  
              
            public void onClick(View view) {  
                String moblileString = mobileNoEditText.getText().toString();  
                String messageString = messageEditText.getText().toString();  
                SmsManager smsManager = SmsManager.getDefault();  
                PendingIntent sendingIntent = PendingIntent.getBroadcast(SmsautoActivity.this, 0, new Intent(), 0);  
                List<String> msgsList = smsManager.divideMessage(messageString);  
                for (String msg : msgsList) {  
                    smsManager.sendTextMessage(moblileString, null, msg, sendingIntent, null);  
                }  
                Toast.makeText(SmsautoActivity.this, "短信发送成功", Toast.LENGTH_LONG).show();  
            }  
        });  
          
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.auto"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".SmsautoActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
	<uses-permission android:name="android.permission.SEND_SMS" />
</manifest>

PS:记得加入权限<uses-permissionandroid:name="android.permission.SEND_SMS"/>

</manifest>

相关推荐