六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 67|回复: 0

sendSMS

[复制链接]

升级  15.33%

68

主题

68

主题

68

主题

举人

Rank: 3Rank: 3

积分
246
 楼主| 发表于 2013-1-15 02:20:18 | 显示全部楼层 |阅读模式
这是自己写的一个发短信的程序
1.编辑TinySMS.java
//TinySMS.java
package com.TinySMS;

import android.app.Activity;
import android.content.SharedPreferences.Editor;
import android.view.*;
import android.os.Bundle;
import android.widget.*;
import android.telephony.PhoneNumberUtils;
import android.telephony.SmsManager;
import android.app.PendingIntent;
import android.content.Intent;
public class TinySMS extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final EditText et1 = (EditText)this.findViewById(R.id.phoneNumber);
        final EditText et2 = (EditText)this.findViewById(R.id.sms);
        final Button btn = (Button)this.findViewById(R.id.send);
        btn.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
        String phone = et1.getText().toString();
        String message = et2.getText().toString();
        if(PhoneNumberUtils.isGlobalPhoneNumber(phone)&&message.length()>0) {
        //给指定号码发送短信
        sendSMS(phone,message);//自己写的方法
        }else {
        Toast.makeText(TinySMS.this, "你输入的电话号码有误,请重新输入", Toast.LENGTH_LONG).show();
        }
        }
        });
    }
    private void sendSMS(String phone,String message) {
    PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this,TinySMS.class), 0);//这个PendingIntent可以理解为一个又封装了一层的Intent,详细用法可见sdk开发文档
    SmsManager sms = SmsManager.getDefault();//通过SmsManager类的静态方法得到一个类的实例对象
    sms.sendTextMessage(phone, null, message, pi, null);//这是这个类发短信的方法
    }
}

2.编辑main.xml
<!--main.xml-->
<?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:id="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="输入电话号码"
/>
    <EditText
android:id="@+id/phoneNumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

<TextView
android:id="@+id/tv2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="输入短信内容"
/>
<EditText
android:id="@+id/sms"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/send"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="发送短信"
/>
</LinearLayout>

3.最后在AndroidMenifest.xml中加入 <uses-permission android:name="android.permission.SEND_SMS"/>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表