初识安卓小程序(Android短信发送器)

2024-06-03 00:08

本文主要是介绍初识安卓小程序(Android短信发送器),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

首先,先创建一个安卓项目(我的版本是4.4.2的),名字为"短信发送器"

然后在res文件夹下找到layout文件夹,找到activity_main.xml或fragment_main.xml,在里面输入或拖拽按钮

<RelativeLayout xmlns: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:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.csdn.sms.MainActivity$PlaceholderFragment" ><TextViewandroid:id="@+id/pl_input_number"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:text="@string/please_input_number"android:textAppearance="?android:attr/textAppearanceMedium" /><EditTextandroid:id="@+id/input_number"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_below="@+id/pl_input_number"android:layout_marginTop="22dp"android:ems="10"android:inputType="phone" /><TextViewandroid:id="@+id/pl_content"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignLeft="@+id/input_number"android:layout_below="@+id/input_number"android:layout_marginTop="47dp"android:text="@string/dx_content"android:textAppearance="?android:attr/textAppearanceLarge" /><EditTextandroid:id="@+id/content"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/pl_content"android:layout_below="@+id/pl_content"android:layout_marginTop="22dp"android:ems="10"android:inputType="textMultiLine" ><requestFocus /></EditText><Buttonandroid:id="@+id/dx_send"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/content"android:layout_below="@+id/content"android:layout_marginTop="64dp"android:text="@string/dx_send" /></RelativeLayout>

最后在src下的java文件里MainActivity.java

package com.csdn.sms;import java.util.ArrayList;import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;public class MainActivity extends Activity {private Button dx_send;private EditText dx_content, dx_number;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.fragment_main);dx_content = (EditText) this.findViewById(R.id.content);dx_number = (EditText) this.findViewById(R.id.input_number);dx_send = (Button) this.findViewById(R.id.dx_send);//第一种方法:通过点击事件来完成dx_send.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.dx_send:String content = dx_content.getText().toString().trim();String number = dx_number.getText().toString().trim();if (TextUtils.isEmpty(content) || TextUtils.isEmpty(number)) {Toast.makeText(MainActivity.this, "电话号码不能为空",Toast.LENGTH_SHORT).show();return;} else {// 短信管理器SmsManager smsManager = SmsManager.getDefault();// 如果超过字数限制,则自动拆分短信ArrayList<String> c = smsManager.divideMessage(content);for (String str : c) {smsManager.sendTextMessage(number, null, str, null,null);}}break;}}});}//第二种:通过让该类实现 OnClickListener接口来重写方法完成,和上面点击方法的内容是一样的@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}}


这篇关于初识安卓小程序(Android短信发送器)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1025477

相关文章

Django开发时如何避免频繁发送短信验证码(python图文代码)

《Django开发时如何避免频繁发送短信验证码(python图文代码)》Django开发时,为防止频繁发送验证码,后端需用Redis限制请求频率,结合管道技术提升效率,通过生产者消费者模式解耦业务逻辑... 目录避免频繁发送 验证码1. www.chinasem.cn避免频繁发送 验证码逻辑分析2. 避免频繁

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

python编写朋克风格的天气查询程序

《python编写朋克风格的天气查询程序》这篇文章主要为大家详细介绍了一个基于Python的桌面应用程序,使用了tkinter库来创建图形用户界面并通过requests库调用Open-MeteoAPI... 目录工具介绍工具使用说明python脚本内容如何运行脚本工具介绍这个天气查询工具是一个基于 Pyt

Ubuntu设置程序开机自启动的操作步骤

《Ubuntu设置程序开机自启动的操作步骤》在部署程序到边缘端时,我们总希望可以通电即启动我们写好的程序,本篇博客用以记录如何在ubuntu开机执行某条命令或者某个可执行程序,需要的朋友可以参考下... 目录1、概述2、图形界面设置3、设置为Systemd服务1、概述测试环境:Ubuntu22.04 带图