吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 967|回复: 0
收起左侧

[已解决] 求java或kotlin大佬解决萌新问题

[复制链接]
行走 发表于 2020-6-21 16:03
本帖最后由 行走 于 2020-6-21 20:37 编辑

上代码
[Asm] 纯文本查看 复制代码
package com.example.sms_cqa

import android.Manifest
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.telephony.SmsMessage
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import okhttp3.*
import java.text.SimpleDateFormat
import java.util.*


private const val TAG = "MyBroadcastReceiver"
class MainActivity : AppCompatActivity() {
    val listener = EchoWebSocketListener()
    val request: Request = Request.Builder()
        .url("ws://网址/api")
        .addHeader("Authorization","Bearer gSGIgfdsosGIYgfds").build()
    val client = OkHttpClient()
    val websocket = client.newWebSocket(request, listener)

    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        
        //        判断并获取权限
        var smsPermissions = arrayOf("Manifest.permission.RECEIVE_SMS")
        var isSmsGranted =
            android.content.pm.PackageManager.PERMISSION_DENIED == ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.RECEIVE_SMS
            )
        if (isSmsGranted) {
            ActivityCompat.requestPermissions(this, smsPermissions, 1)
        }

        smsPermissions = arrayOf("Manifest.permission.READ_SMS")
        isSmsGranted =
            android.content.pm.PackageManager.PERMISSION_DENIED == ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.READ_SMS
            )
        if (isSmsGranted) {
            ActivityCompat.requestPermissions(this, smsPermissions, 1)
        }
        //        注册广播接收器
        val br: BroadcastReceiver = MyBroadcastReceiver()
        val filter = IntentFilter().apply {
            addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED)
            addAction("android.provider.Telephony.SMS_RECEIVED")
            priority = 1000
        }
        registerReceiver(br, filter)
        }
    }
    class EchoWebSocketListener : WebSocketListener() {
        override fun onOpen(webSocket: WebSocket, response: Response) {
//        发送
        }

        override fun onMessage(webSocket: WebSocket, text: String) {
//        接收
            //  logger.e("onMessage","onMessage: " + text);
        }
        override fun onClosing(webSocket: WebSocket, code: Int, reason: String) {
//            远程端暗示没有数据交互时回调
//            webSocket.close(1000, null)
        }


        override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
            //服务器关闭后
        }

        override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
            //服务器中断
            Log.e("onFailure","onFailure: " + t.toString()+response.toString())
        }
    }

    private class MyBroadcastReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            val objs = intent.extras!!["pdus"] as Array<Any>?
            for (obj in objs!!) {
                val pdu = obj as ByteArray
                val sms: SmsMessage = SmsMessage.createFromPdu(pdu)
                // 短信的内容
                var message = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date()) + "\n发件人:" + sms.getOriginatingAddress() + "\n" + sms.getMessageBody()

            }
        }
    }



额,已解决,问题的根本其实是我在json里发换行符了,把\n换为\\n即可

各位大佬,kotlin萌新求助,我想通过短信广播接收短信然后通过websocket发送到服务器,然后,我就是被接收到短信后怎么用websocket给发出去难住了,没吾爱币了,所以到水区求助。

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-1-15 20:49

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表