Android中实现短信发送的一种方式

来源:互联网 发布:linux编译命令 编辑:程序博客网 时间:2024/06/12 05:26

代码是小马写的,我只是借来用用~(@^_^@)~
在此基础上可以实现以下功能:

  1. 发送特定指令到指定对象
  2. 实现短信群发
  3. 定时自动发送信息

SendSmsActivity.java:

package com.test.smsmangerdemo.sendsmsactivity;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.content.Intent;import android.app.PendingIntent;import android.telephony.SmsManager;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;/** *发送短信实例 */public class SendSmsActivity extends AppCompatActivity {    EditText phone, content;    Button send;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_send_sms);        //获取 SMSManger管理器        final SmsManager smsManager = SmsManager.getDefault();        //初始化控件        phone = (EditText) findViewById(R.id.et_phone);        content = (EditText) findViewById(R.id.et_content);        send = (Button) findViewById(R.id.btn_send);        send.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //创建一个 android.app.PendingIntent 对象                PendingIntent pi = PendingIntent.getActivity(SendSmsActivity.this, 0, new Intent(), 0);                //发送短信                smsManager.sendTextMessage(phone.getText().toString(), null, content.getText().toString(),                        pi, null);                //提示短信发送完成                Toast.makeText(SendSmsActivity.this, "短信发送完成", Toast.LENGTH_SHORT).show();            }        });    }}

AndroidMainfest.xml:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.test.smsmangerdemo.sendsmsactivity" >    <uses-permission android:name="android.permission.SEND_SMS"/>    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme" >        <activity android:name=".SendSmsActivity" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

activity_send_sms.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.test.smsmangerdemo.sendsmsactivity.SendSmsActivity"><LinearLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="horizontal">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="收件人"/>    <EditText        android:id="@+id/et_phone"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="10dp"/></LinearLayout><LinearLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginTop="10dp"    android:orientation="horizontal">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="发送内容"/>    <EditText        android:id="@+id/et_content"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="10dp"        android:gravity="top"        android:lines="5"        android:text="你好"/></LinearLayout><Button    android:layout_gravity="center_horizontal"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="发送"    android:id="@+id/btn_send"    /></LinearLayout>

实现效果:

这里写图片描述

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 海尔超低温保存箱低温报警怎么办 孕期吃了烧烤蔬菜怎么办 百姓动迁政府说话不算数怎么办 孕妇用了青草膏怎么办 药材不知道啥名怎么办 诛仙手游鸿蒙紫气不见了怎么办 紫叶兰叶干了怎么办 绿萝叶子中毒了怎么办? 怀孕初期胃疼的厉害怎么办 怀孕胃疼的厉害怎么办 天然气热水器风口一封闭怎么办 热水器着火后火变大怎么办? 人如果没有天魂怎么办 被鬼魂附体了该怎么办 被吸血蠓咬了怎么办 股份公司大股东占用资金怎么办 抱小孩抱的胳膊疼怎么办 一个月好几个性伴侣怎么办 惹了势力大的人怎么办 给佛像换新法器旧的怎么办 cad填充图案拾取不了点怎么办 cad填充图案太密怎么办 桃木葫芦挂件裂口了怎么办 皮肤被太阳晒伤变黑怎么办 皮肤又粗又黑该怎么办 盆栽红薯长疯了怎么办 四叶草叶子发黄怎么办 美篇不想让别人看到怎么办 黑魂3暗灵入侵怎么办 问道手游账号忘了怎么办 问道手游密码忘了怎么办 毒蚂蚁咬了红肿怎么办 电脑开机后有电流声怎么办 小学生阅读方面不会总结怎么办 天龙八部手游转职装备怎么办 投简历没回应该怎么办? 开车开的腰疼怎么办 开车腰疼怎么办吃什么 58简历投错了怎么办 开了一天车腰疼怎么办 开车腰疼怎么办怎么解决好