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

原创粉丝点击