初识安卓小程序(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

相关文章

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

将Java程序打包成EXE文件的实现方式

《将Java程序打包成EXE文件的实现方式》:本文主要介绍将Java程序打包成EXE文件的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录如何将Java程序编程打包成EXE文件1.准备Java程序2.生成JAR包3.选择并安装打包工具4.配置Launch4

Java程序进程起来了但是不打印日志的原因分析

《Java程序进程起来了但是不打印日志的原因分析》:本文主要介绍Java程序进程起来了但是不打印日志的原因分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java程序进程起来了但是不打印日志的原因1、日志配置问题2、日志文件权限问题3、日志文件路径问题4、程序

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me