Activity 主程序
package cn.csdn.zb;
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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class NawsActivity extends Activity {
/** Called when the activity is first created. */
Button bt;
EditText edit;
EditText edit2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取试图组件
bt=(Button) findViewById(R.id.button1);
edit=(EditText) findViewById(R.id.et1);
edit2=(EditText) findViewById(R.id.et2);
//注册按钮点击事件
bt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//获取电话号码
String phone= edit.getText().toString();
//获取信息内容
String naws =edit2.getText().toString();
//短信管理器
SmsManager smsManager =SmsManager.getDefault();
//声明发信息意图
PendingIntent intent= PendingIntent.getBroadcast(NawsActivity.this, 0, new Intent(), 0);
//注意,当短信内容过长时要用短信管理器拆分
if(naws.length()>30)
{ // 将一条长信息拆分为多条小于50的,返回的是一个集合
List<String> count = smsManager.divideMessage(naws);
//循环发送方
for(String sms :count)
{
//执行意图 ,有一个信息发送管理器
smsManager.sendTextMessage(phone, null, sms, intent, null);
}
}else
{
smsManager.sendTextMessage(phone, null,naws , intent, null);
}
Toast.makeText(NawsActivity.this, "发送成功", Toast.LENGTH_SHORT).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="输入要拨打的电话号码" />
<EditText
android:id="@+id/et1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
< TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="输入短信内容" />
<EditText
android:id="@+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPostalAddress" >
< requestFocus />
< /EditText>
< Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送此信息" />
< /LinearLayout>
项目清单配置
< ?xml version="1.0" encoding="utf-8"?>
< manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.csdn.zb"
android:versionCode="1"
android:versionName="1.0" >
< uses-sdk android:minSdkVersion="10" />
< uses-permission android:name="android.permission.SEND_SMS"/>
< application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
< activity
android:name=".NawsActivity"
android:label="@string/app_name" >
< intent-filter>
< action android:name="android.intent.action.MAIN" />
< category android:name="android.intent.category.LAUNCHER" />
< /intent-filter>
< /activity>
< /application>
< /manifest>
package cn.csdn.zb;
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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class NawsActivity extends Activity {
/** Called when the activity is first created. */
Button bt;
EditText edit;
EditText edit2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取试图组件
bt=(Button) findViewById(R.id.button1);
edit=(EditText) findViewById(R.id.et1);
edit2=(EditText) findViewById(R.id.et2);
//注册按钮点击事件
bt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//获取电话号码
String phone= edit.getText().toString();
//获取信息内容
String naws =edit2.getText().toString();
//短信管理器
SmsManager smsManager =SmsManager.getDefault();
//声明发信息意图
PendingIntent intent= PendingIntent.getBroadcast(NawsActivity.this, 0, new Intent(), 0);
//注意,当短信内容过长时要用短信管理器拆分
if(naws.length()>30)
{ // 将一条长信息拆分为多条小于50的,返回的是一个集合
List<String> count = smsManager.divideMessage(naws);
//循环发送方
for(String sms :count)
{
//执行意图 ,有一个信息发送管理器
smsManager.sendTextMessage(phone, null, sms, intent, null);
}
}else
{
smsManager.sendTextMessage(phone, null,naws , intent, null);
}
Toast.makeText(NawsActivity.this, "发送成功", Toast.LENGTH_SHORT).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="输入要拨打的电话号码" />
<EditText
android:id="@+id/et1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
< TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="输入短信内容" />
<EditText
android:id="@+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPostalAddress" >
< requestFocus />
< /EditText>
< Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送此信息" />
< /LinearLayout>
项目清单配置
< ?xml version="1.0" encoding="utf-8"?>
< manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.csdn.zb"
android:versionCode="1"
android:versionName="1.0" >
< uses-sdk android:minSdkVersion="10" />
< uses-permission android:name="android.permission.SEND_SMS"/>
< application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
< activity
android:name=".NawsActivity"
android:label="@string/app_name" >
< intent-filter>
< action android:name="android.intent.action.MAIN" />
< category android:name="android.intent.category.LAUNCHER" />
< /intent-filter>
< /activity>
< /application>
< /manifest>