android发短信小例子TinySMS
来源:互联网 发布:linux设置自动挂载 编辑:程序博客网 时间:2024/05/17 07:19
TinySMS.java
------------------------------------------------------------------------------
package com.mars.android.TinySMS;
//发短信 5-16
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class TinySMS extends Activity {
/** Called when the activity is first created. */
EditText phoneno=null;
EditText message=null;
Button send=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
phoneno=(EditText)findViewById(R.id.phoneno);
message=(EditText)findViewById(R.id.msgcontent);
send=(Button)findViewById(R.id.sendmsg);
send.setOnClickListener(new sendmessage());
}
class sendmessage implements android.view.View.OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String phone=phoneno.getText().toString();
String msg=message.getText().toString();
if(PhoneNumberUtils.isGlobalPhoneNumber(phone)&&phone.length()>0){
sendSMS(phone,msg);
}
else
Toast.makeText(TinySMS.this, "短信发送失败,请重新输入号码和短信内容!", Toast.LENGTH_LONG).show();
}
}
/**
* SmsManager 是android.telephony.gsm.SmsManager中定义的用户管理短信应用的类。
* 开发人员不必实例化SmsManager类,只需要调用静态方法getDefault()获得SmsManager对象,
* PendingIntent对象用于指向TinySMSActivity.当用户按下‘发送短信’按键后,用户界面
* 重新回到TinySMS的初始界面
* @param phonenumber 电话号码
* @param msg 短信内容
*/
public void sendSMS(String phonenumber,String msg){//发送短信的类
PendingIntent pi=PendingIntent.getActivity(this, 0, new Intent(this,TinySMS.class), 0);
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(phonenumber, null, msg, pi, null);//发送信息到指定号码
}
}
-------------------------------------------------------------------------------------------------------------------------------------------
以下为布局文件代码
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入短信号码:"
/>
<EditText
android:id="@+id/phoneno"
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/msgcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/sendmsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="发送短信"
/>
</LinearLayout>
--------------------------------------------------------------------------------------------------------------
发短信需要权限,在manifest.xml中添加发短信的权限。如下
<uses-permission android:name="android.permission.SEND_SMS"/>
- android发短信小例子TinySMS
- 发短信的小例子(Android)
- android 发送短信小例子
- android小程序之打电话、发短信
- 发短信小项目
- Android开发短信备份小例子
- android打电话,发短信
- android打电话,发短信
- android 打电话 发短信
- android打电话,发短信
- Android发短信程序
- Android 发短信事例
- 模拟android发短信
- android 打电话发短信
- (Android review)发短信
- Android打电话&发短信
- android发短信操作
- Android发短信
- 迷茫时刻
- DNS故障分析【转】
- 算法题15 穿越沙漠问题,飞机加油问题
- android SharedPreferences使用
- Linux下有趣的Banner生成工具。
- android发短信小例子TinySMS
- 信号接收过程
- 错误代码 转http://blogs.msdn.com/b/joshpoley/archive/2008/01/08/errors-0ee-facility-rtc-interface.aspx
- ORA-01034&ORA-27101错误
- 如何打印并分页显示带有滚动条的datagridview
- Json串到json对象的转换
- c#的事件
- stagefright与opencore对比
- 算法题16 二叉树最近公共父节点