美文网首页
android 手动群发短信

android 手动群发短信

作者: 安卓_背包客 | 来源:发表于2020-03-14 22:06 被阅读0次
  <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>


package com.example.myapplication

import android.app.PendingIntent
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.telephony.SmsManager
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    private var mArray: MutableList<String>? = null
    private var mAdapter: PhoneAdapter? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initEvent()
        clearData()
        initListener()
        initAdapter()
        initViewModel()
    }

    private fun initAdapter() {
        mAdapter = PhoneAdapter(mContext = this, phonelist = mArray!!)
        val manger = GridLayoutManager(this, 1)
        rlv_content.layoutManager = manger
        rlv_content.adapter = mAdapter
    }

    private fun initEvent() {

    }

    private fun initListener() {
        btn_add.setOnClickListener {
            val com = et_input_phone.text.toString()
            val list = mutableListOf<String>()
            list.add(com)
            addData(list = list)
            mAdapter?.also {
                it.notifyDataSetChanged()
            }
        }
        tv_send_phone.setOnClickListener {
            val input = et_input_content.text.toString()
            if (mArray.isNullOrEmpty()) return@setOnClickListener
            mArray?.let { data ->
                val strbuf = StringBuffer()
                for (index in 0 until data.size) {
                    strbuf.append(data[index])
                    if (index != data.size - 1)
                        strbuf.append(";")
                }
                sendPhoneMsg(strbuf.toString(),input)
            }
        }
    }

    private fun sendPhoneMsg(phone: String, input: String) {
        val sIntent = Intent("send_sms")
        val sentIntent =
            PendingIntent.getBroadcast(this, 0, sIntent, 0) //短信成功发送后才发送该广播
        val dIntent = Intent("deliver_sms")
        val deliveryIntent =
            PendingIntent.getBroadcast(this, 1, dIntent, 0) //短信成功接收后才发送该广播

        val default = SmsManager.getDefault()
        default.sendTextMessage(phone,null,"$input",sentIntent,deliveryIntent)


        val smsToUri = Uri.parse("smsto:" + phone);
        val intent = Intent(Intent.ACTION_SENDTO, smsToUri);
        intent.putExtra("sms_body", "$input");
        startActivity(intent);

    }

    private fun initViewModel() {

    }

    private fun clearData() {
        if (mArray == null) {
            mArray = ArrayList()
        } else {
            mArray!!.clear()
        }
    }

    private fun addData(list: MutableList<String>?) {
        if (list == null || list.isEmpty()) {
            return
        }
        if (mArray == null) {
            clearData()
        }
        mArray!!.addAll(list)
    }

}


package com.example.myapplication

import android.content.Context
import android.text.Layout
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

/**
 * @Author : YFL  is Creating a porject in My Application
 * @Package com.example.myapplication
 * @Email : yufeilong92@163.com
 * @Time :2020/3/14 14:29
 * @Purpose :手机适配器
 */
class PhoneAdapter(var mContext: Context, var phonelist: MutableList<String>) :
    RecyclerView.Adapter<PhoneAdapter.ViewHodler>() {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhoneAdapter.ViewHodler {
        val view = LayoutInflater.from(mContext).inflate(R.layout.item_phone_list, null)
        return ViewHodler(view)
    }

    override fun getItemCount(): Int {
        return phonelist.size
    }

    override fun onBindViewHolder(holder: PhoneAdapter.ViewHodler, position: Int) {
        val s = phonelist[position]
        holder.tvPhone.text = s

    }

    inner class ViewHodler(var view: View) : RecyclerView.ViewHolder(view) {
        val tvPhone = view.findViewById<TextView>(R.id.tv_phone_title)

    }
}

相关文章

网友评论

      本文标题:android 手动群发短信

      本文链接:https://www.haomeiwen.com/subject/mjposhtx.html