热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

5.模拟短信发送

代码实现模拟短信的发送。主要的操作步骤有:1.获取短信管理器SmsManagersmSmsManager.getDefault();2.发送短信sm.send

代码实现模拟短信的发送。主要的操作步骤有:

//1.获取短信管理器

SmsManager sm = SmsManager.getDefault();

//2.发送短信

sm.sendTextMessage(phone, null, content, null, null);

模拟的代码如下

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void send(View v){EditText et1 = (EditText) findViewById(R.id.et1);EditText et2 = (EditText) findViewById(R.id.et2);String phOne= et1.getText().toString();String cOntent= et2.getText().toString();//1.获取短信管理器SmsManager sm = SmsManager.getDefault();//2.发送短信//sm.sendTextMessage(phone, null, content, null, null);//短信内容很长的时候分成几个发送ArrayList smss = sm.divideMessage(content);for(String string:smss){sm.sendTextMessage(phone, null, string, null, null);}}

SDK>23的时候代码需要下面的

activity_main.xml


MainActivity.java

package com.ldw.test;import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;import java.util.ArrayList;public class MainActivity extends Activity {private Button btn_send;private Button btn_dail;private EditText et_number;private EditText et_content;private static final int PERMISSION_REQUEST_CODE = 1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {if (checkSelfPermission(Manifest.permission.SEND_SMS)== PackageManager.PERMISSION_DENIED) {Log.d("permission", "permission denied to SEND_SMS - requesting it");String[] permissiOns= {Manifest.permission.SEND_SMS};requestPermissions(permissions, PERMISSION_REQUEST_CODE);}if (checkSelfPermission(Manifest.permission.CALL_PHONE)== PackageManager.PERMISSION_DENIED) {Log.d("permission", "permission denied to CALL_PHONE - requesting it");String[] permissiOns= {Manifest.permission.CALL_PHONE};requestPermissions(permissions, PERMISSION_REQUEST_CODE);}}btn_send = (Button) findViewById(R.id.btn_send);btn_send.setOnClickListener(new MyListener());}class MyListener implements View.OnClickListener{@Overridepublic void onClick(View v){et_cOntent= (EditText) findViewById(R.id.et_content);et_number = (EditText) findViewById(R.id.et_number);String cOntent= et_content.getText().toString();String number = et_number.getText().toString();System.out.println("===="+number);System.out.println("===="+content);SmsManager sm = SmsManager.getDefault();ArrayList smss = sm.divideMessage(content);for (String string:smss){sm.sendTextMessage(number, null, string, null, null);}}}public void dail(View v){System.out.println("==============");et_number = (EditText) findViewById(R.id.et_number);String number = et_number.getText().toString();Intent intent = new Intent();intent.setAction(intent.ACTION_CALL);intent.setData(Uri.parse("tel:" + number));startActivity(intent);}}

Manifist.xml


 

 


推荐阅读
author-avatar
mobiledu2502858407
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有