博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android发送短信样例
阅读量:7174 次
发布时间:2019-06-29

本文共 2261 字,大约阅读时间需要 7 分钟。

Android应用开发中我们经常须要发送手机短信。这对于android平台来说,是最简单只是的功能了,无需太多代码,也无需自己定义代码,仅仅须要调用android提供的消息管理类SmsManager就能够了。

【源代码下载】

核心就是使用SmsManager的sendTextMessage方法加上PendingIntent跳转。

核心代码例如以下:

SmsManager sms=SmsManager.getDefault();PendingIntent  intent=PendingIntent.getBroadcast(MainActivtiy.this,0, new Intent(), 0);sms.sendTextMessage(phone.getText().toString(), null, text.getText().toString(), intent, null);

以下一起来实现这个功能:

第1步:新建一个activity MainActivtiy

import android.app.Activity;import android.app.PendingIntent;import android.content.Intent;import android.os.Bundle;import android.telephony.SmsManager;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivtiy extends Activity {EditText text;EditText phone;Button send;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);send=(Button)findViewById(R.id.send);text=( EditText)findViewById(R.id.text);phone=( EditText)findViewById(R.id.phone);send.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {SmsManager sms=SmsManager.getDefault();PendingIntent  intent=PendingIntent.getBroadcast(MainActivtiy.this,0, new Intent(), 0);sms.sendTextMessage(phone.getText().toString(), null, text.getText().toString(), intent, null);Toast.makeText( MainActivtiy.this, "发送成功.....", Toast.LENGTH_LONG).show();}});}}

2步:改动配置文件:main.xml

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" > <EditText android:id="@+id/phone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请输入电话号码" android:inputType="phone" android:text="" > </EditText> <EditText android:id="@+id/text" android:inputType="text" android:hint="请输入消息" android:layout_width="fill_parent" android:layout_height="wrap_content" > </EditText> <Button android:id="@+id/send" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="发送消息" > </Button> </LinearLayout>

3步:在配置文件AndroidManifest.xml中加入发送短信支持

第4步调试执行:

【源代码下载】

转载地址:http://wbbzm.baihongyu.com/

你可能感兴趣的文章
-bash: git: command not found
查看>>
关于CCIE R&S v5 的一些消息
查看>>
学习笔记---Ubuntu上搭建web服务器
查看>>
在企业里面如何部署证书服务
查看>>
Linux中VMware虚拟机增加磁盘空间的扩容操作
查看>>
Oracle Virtual Box 安装64位的Linux遇到CPU不适用错误
查看>>
Azure实践之Azure monitor简介及如何为北二东二VM创建alert
查看>>
LVS集群
查看>>
nio
查看>>
Hibernate主键生成策略
查看>>
Fedora升级操作,轻易不要升级,如果升级请规范操作
查看>>
oracle的各种视图
查看>>
多个线程访问共享数据
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
2019 年,容器技术生态会发生些什么?
查看>>
linux公司常用基础命令必知必会一
查看>>
js正则
查看>>
Linux命令----sed和awk
查看>>
如何删除Exchange 2013默认数据库
查看>>