android 发送短信小例子

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"/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值