android开发第一弹:短信SMS
来源:互联网 发布:h3c查看端口vlan 编辑:程序博客网 时间:2024/05/16 05:35
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" 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.example.sendsms.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="号\u3000码:" android:textSize="22sp"/> <EditText android:id="@+id/et_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入号码"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="内\u3000容:" android:textSize="22sp"/> <EditText android:id="@+id/et_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入内容"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.7" android:orientation="horizontal"> <Button android:id="@+id/bt_send" android:layout_width="match_parent" android:layout_height="match_parent" android:text="发\u3000送" android:textSize="22sp" android:background="@drawable/border"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="4" android:orientation="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="没有号码?" android:textSize="22sp" android:background="@null" android:layout_gravity="center" android:layout_marginLeft="30dp" android:layout_marginTop="40dp"/> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> <Button android:id="@+id/bt_directsend" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="直接发送" android:textSize="22sp" android:layout_gravity="center" android:layout_marginLeft="30dp" android:layout_marginTop="40dp" android:background="@drawable/bg_round_rectangle"/> </LinearLayout> </LinearLayout></LinearLayout>
MainActivity.java
package com.example.sendsms;import android.app.Activity;import android.os.Bundle;import android.telephony.SmsManager;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import java.util.ArrayList;public class MainActivity extends Activity { //声明变量 EditText et_number,et_content; Button bt_send,bt_directsend; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //绑定布局 setContentView(R.layout.activity_main); //初始化 init(); } //实例化变量 private void init() { et_number = (EditText) findViewById(R.id.et_number); et_content = (EditText) findViewById(R.id.et_content); bt_send = (Button) findViewById(R.id.bt_send); bt_directsend = (Button) findViewById(R.id.bt_directsend); //给bt_send按钮设置点击监听器 bt_send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //获得手机号码 String number = et_number.getText().toString().trim(); //获得短信内容 String content = et_content.getText().toString().trim(); if (number.equals("")){ Toast.makeText(MainActivity.this,"请输入手机号码",Toast.LENGTH_SHORT).show(); return; } //获得短信消息管理器 SmsManager smsManager = SmsManager.getDefault(); //判断内容是否大于60个字 if (content.length()>60){ //分割内容 ArrayList<String> msgs = smsManager.divideMessage(content); for (String msg:msgs){ //发送短信 smsManager.sendTextMessage(number,null,msg,null,null); } }else { //发送内容 smsManager.sendTextMessage(number,null,content,null,null); } Toast.makeText(MainActivity.this,"发送完成",Toast.LENGTH_SHORT).show(); } }); //给bt_directsend设置点击监听器 bt_directsend.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //获得手机号码 String number = et_number.getText().toString().trim(); //获得短信内容 String content = et_content.getText().toString().trim(); if (number.equals("")){ Toast.makeText(MainActivity.this,"请输入手机号码",Toast.LENGTH_SHORT).show(); return; } //获得短信消息管理器 SmsManager smsManager = SmsManager.getDefault(); //判断内容是否大于60个字 if (content.length()>60){ //分割内容 ArrayList<String> msgs = smsManager.divideMessage(content); for (String msg:msgs){ //发送短信 smsManager.sendTextMessage(number,null,msg,null,null); } }else { //发送内容 smsManager.sendTextMessage(number,null,content,null,null); } Toast.makeText(MainActivity.this,"发送完成",Toast.LENGTH_SHORT).show(); } }); }}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sendsms"> //注册发送短信的权限 <uses-permission android:name="android.permission.SEND_SMS"/>
<application android:allowBackup="true" android:icon="@drawable/type_big_5" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
0 0
- android开发第一弹:短信SMS
- android开发之发送短信SMS
- SMS短信开发技术
- android 发送短信sms
- Android 之 sms 短信
- Android 发送短信 sms
- Android SMS 短信操作
- Android 之 sms 短信
- android SMS 发送短信
- Android SMS 短信读取
- Android 监听SMS短信
- Android 之 sms 短信
- Android Sms短信发送
- Android 4.4 如何开发自己的短信 SMS App
- Android开发之电话服务、SMS短信监听
- android sms(短信)详解
- SMS---android 发送短信 详解
- android 短信(SMS)结构解析
- MyEclipse汉字乱码问题
- poj 1611 并查集
- 树状数组(二)与poj2155
- AlertDialog对话框的简单使用
- MacOS不可或缺的套件管理工具---Homebrew
- android开发第一弹:短信SMS
- 工控安全漏洞举例
- (Sequence_Stack,pattern)数据结构实验之栈八:栈的基本操作
- 个人记录-LeetCode 87. Scramble String
- 问题讨论:超市取消称重台,在收银台称重收钱,并且只有1/3的收银台称重
- Android通过崩溃日志中控件id定位崩溃位置的方法
- Navicat连接Oracle数据库报错(ORA-28547)
- STL6大标准库的关系和作用
- 文件操作、文件搜索