作者:mobiledu2502858407 | 来源:互联网 | 2023-08-17 16:03
代码实现模拟短信的发送。主要的操作步骤有: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