android发送短信实例
来源:互联网 发布:深夜福利直播软件 编辑:程序博客网 时间:2024/06/05 17:04
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>
- android发送短信实例
- Android自定义发送短信实例
- Android 简单发送短信实例
- [Android实例] android 发送短信和打电话
- [Android实例] android如何监听发送短信?
- android sdk--- 发送短信实例学习
- 发送短信实例
- java发送短信实例
- Android监听器之监听短信发送消息实例
- android 中发送短信
- android中发送短信
- android发送短信
- Android 发送短信
- Android自动发送短信
- Android发送短信
- android 发送短信sms
- android 发送短信
- android发送短信
- 编写一个项目,实现页面之间切换时的动画效果。上传代码并描述步骤
- CSliderCtrl::ClearSel
- window 批处理教程
- C++愤恨者札记8—— 常引用参数也不可靠的情况
- CSliderCtrl::ClearTics
- android发送短信实例
- Windows 8 Directx 开发学习笔记(十四)使用几何着色器实现三角形细分
- 仿计算器布局代码。(应用各种布局)
- 安卓简述activity生命周期
- 【小蒙淘金】1.15金评-黄金白银行情分析及操作建议
- 安卓仿计算器布局代码
- 安卓组件
- 用Backtrack进行渗透测试评估
- 【usaco】MAR10