android模拟器发送短信
编程建立一个短信发送界面。提供输入手机号码和短信内容,利用系统服务,发送短信内容到另一个模拟器。代码布局文件<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.a
·
编程建立一个短信发送界面。提供输入手机号码和短信内容,利用系统服务,
发送短信内容到另一个模拟器。
效果:
代码
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请输入手机号"
android:id="@+id/text"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请输入短信内容"
android:id="@+id/text1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送"
android:id="@+id/button"/>
</LinearLayout>
activity文件
package com.example.lab4exam02;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
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;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Button mButton1;
private EditText mEditText1;
private EditText mEditText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.SEND_SMS} , 1);*/
checkPermission();
//获取资源
mEditText1 = (EditText)findViewById(R.id.edit);
mEditText2 = (EditText)findViewById(R.id.edit1);
mButton1 = (Button)findViewById(R.id.button);
//发送短信的响应
mButton1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
//获取发送地址和发送内容
String messageAddress = mEditText1.getText().toString();
String messageContent = mEditText2.getText().toString();
//构建一取得default instance的SmsManager对象
SmsManager smsManager = SmsManager.getDefault();
//检查输入内容是否为空,这里为了简单就没有判断是否是号码,短信内容长度的限制也没有做
if(messageAddress.trim().length()!=0 && messageContent.trim().length()!=0)
{
//设置短信长度
if(messageContent.length() > 70){
ArrayList<String> texts=smsManager.divideMessage(messageContent);
for (String text:texts){
smsManager.sendTextMessage(messageAddress, null, text, null, null);
}
}else {
smsManager.sendTextMessage(messageAddress, null, messageContent, null, null);
}
//提示发送成功
Toast.makeText(MainActivity.this, "发送成功", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(MainActivity.this, "发送地址或者内容不能为空", Toast.LENGTH_SHORT).show();
}
}
});
}
String[] permissions = new String[]{Manifest.permission.SEND_SMS};
List<String> mPermissionList = new ArrayList<>();
// private ImageView welcomeImg = null;
private static final int PERMISSION_REQUEST = 1;
// 检查权限
private void checkPermission() {
mPermissionList.clear();
//判断哪些权限未授予
for (int i = 0; i < permissions.length; i++) {
if (ContextCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
mPermissionList.add(permissions[i]);
}
}
/**
* 判断是否为空
*/
if (mPermissionList.isEmpty()) {//未授予的权限为空,表示都授予了
} else {//请求权限方法
String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);//将List转为数组
ActivityCompat.requestPermissions(this, permissions, PERMISSION_REQUEST);
}
}
/**
* 响应授权
* 这里不管用户是否拒绝,都进入首页,不再重复申请权限
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case PERMISSION_REQUEST:
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
break;
}
}
}
androidManifest配置文件
权限申请
<uses-permission android:name="android.permission.SEND_SMS"/>
问题解析
动态权限申请
在配置文件中申请权限还是会报无权限问题,这里我们采用动态申请权限
方法一:练习时直接在oncreate里面添加该语句
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.SEND_SMS} , 1);
方法二:对方法一的完善
String[] permissions = new String[]{Manifest.permission.SEND_SMS};
List<String> mPermissionList = new ArrayList<>();
// private ImageView welcomeImg = null;
private static final int PERMISSION_REQUEST = 1;
// 检查权限
private void checkPermission() {
mPermissionList.clear();
//判断哪些权限未授予
for (int i = 0; i < permissions.length; i++) {
if (ContextCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
mPermissionList.add(permissions[i]);
}
}
/**
* 判断是否为空
*/
if (mPermissionList.isEmpty()) {//未授予的权限为空,表示都授予了
} else {//请求权限方法
String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);//将List转为数组
ActivityCompat.requestPermissions(this, permissions, PERMISSION_REQUEST);
}
}
/**
* 响应授权
* 这里不管用户是否拒绝,都进入首页,不再重复申请权限
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case PERMISSION_REQUEST:
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
break;
}
}
短信发送
在这里需要控制短信的发送长度,如果长度大于70就分开发送
//获取发送地址和发送内容
String messageAddress = mEditText1.getText().toString();
String messageContent = mEditText2.getText().toString();
//构建一取得default instance的SmsManager对象
SmsManager smsManager = SmsManager.getDefault();
//检查输入内容是否为空
if (messageAddress.trim().length() != 0 && messageContent.trim().length() != 0) {
//设置短信长度
if (messageContent.length() > 70) {
ArrayList<String> texts = smsManager.divideMessage(messageContent);
for (String text : texts) {
smsManager.sendTextMessage(messageAddress, null, text, null, null);
}
} else {
smsManager.sendTextMessage(messageAddress, null, messageContent, null, null);
}
//提示发送成功
Toast.makeText(MainActivity.this, "发送成功", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "发送地址或者内容不能为空", Toast.LENGTH_SHORT).show();
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)