正文  设备功能 > 短信/彩信/SMS/MMS >

android 短信发送器

android短信发送器源码如下:程序主文件 MainActiviy.javapackage com.example.sms;import java.util.ArrayList;import android.os.Bundle; import android.app.Activity; import android.telephony.Sms......

android短信发送器源码如下:

程序主文件 MainActiviy.java

package com.example.sms;

import java.util.ArrayList;

import android.os.Bundle; import android.app.Activity; import android.telephony.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 button;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    numberText = (EditText) this.findViewById(R.id.number);
    contentText = (EditText) this.findViewById(R.id.content);
    button = (Button) this.findViewById(R.id.button);
    button.setOnClickListener(new ButtonClickListener());

}

private final class ButtonClickListener implements View.OnClickListener {

    public void onClick(View arg0) {
        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_LONG).show();
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

此文件是布局管理用与下面文件配套使用string.xml

<string name="app_name">短信发送器</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">短信发送器</string>
<string name="number">请输入手机号</string>
<string name="content">请输入短信内空</string>
<string name="button">发送短信</string>

 

界面布局文件 activity_mian.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" >

<TextView
    android:id="@+id/textView1"
    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"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/number"
    android:text="@string/content" />

<EditText
    android:id="@+id/content"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView2"
    android:ems="10"
    android:minLines="5" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/content"
    android:text="@string/button" />
<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/number" />

 

权限配制文件AndroidManifest.xml

 

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

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <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" />