本帖最后由 行走 于 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给发出去难住了,没吾爱币了,所以到水区求助。 |