如何为 AlarmPingSender.java:76 编写 PendingIntent

问题描述 投票:0回答:1

我有一个应用程序,我在其中连接到 io.adafruit 并订阅和发布主题。 该应用程序运行良好,直到我将 Android Studio 更新到版本 12,现在我不断收到以下错误并崩溃

2022-10-10 15:17:53.905 14953-15048/com.example.mqttkotlinsample D/AlarmPingSender: Register alarmreceiver to MqttServiceMqttService.pingSender.
    
    --------- beginning of crash
2022-10-10 15:17:53.907 14953-15048/com.example.mqttkotlinsample E/AndroidRuntime: FATAL EXCEPTION: MQTT Rec: 
    Process: com.example.mqttkotlinsample, PID: 14953
    java.lang.IllegalArgumentException: com.example.mqttkotlinsample: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
    Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
        at android.app.PendingIntent.checkFlags(PendingIntent.java:401)
        at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:671)
        at android.app.PendingIntent.getBroadcast(PendingIntent.java:658)
        at org.eclipse.paho.android.service.AlarmPingSender.start(AlarmPingSender.java:76)
        at org.eclipse.paho.client.mqttv3.internal.ClientState.connected(ClientState.java:1209)
        at org.eclipse.paho.client.mqttv3.internal.ClientState.notifyReceivedAck(ClientState.java:1045)
        at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:151)
        at java.lang.Thread.run(Thread.java:1012)
2022-10-10 15:17:53.994 14953-14953/com.example.mqttkotlinsample D/com.example.mqttkotlinsample.MQTTClient$defaultCbDisconnect$1: Disconnected
2022-10-10 15:17:53.994 14953-14953/com.example.mqttkotlinsample D/com.example.mqttkotlinsample.ClientFragment$onViewCreated$2: Connection lost null

通过查看日志并使用断点,我得出结论,我需要将 PendingIntent 放在哪里,但我不知道如何编写它。

有人可以帮我吗。

这是我的代码;

package com.example.mqttkotlinsample


import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import android.widget.ImageButton
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import org.eclipse.paho.client.mqttv3.*

class ClientFragment : Fragment() {

    private lateinit var mqttClient : MQTTClient

    override fun onCreate(savedInstanceState: Bundle?) {
        Log.d(this.javaClass.name, "LAUNCHED!!!!!!!!!!!!!!!!!!!")
        super.onCreate(savedInstanceState)

activity?.onBackPressedDispatcher?.addCallback(this, object : OnBackPressedCallback(true) {

            override fun handleOnBackPressed() {

if (mqttClient.isConnected()) {

                    Log.d(this.javaClass.name, "CALLBACK!!!!!!!!!!!!!!!!!!!")

                   // Disconnect from MQTT Broker
                    mqttClient.disconnect(object : IMqttActionListener {
                        override fun onSuccess(asyncActionToken: IMqttToken?) {

                            Log.d(this.javaClass.name, "Disconnected")

                        }

                        override fun onFailure(asyncActionToken: IMqttToken?, exception: Throwable?) {

                            Log.d(this.javaClass.name, "Failed to disconnect")
                        }
                    })
                } else {

                    Log.d(this.javaClass.name, "Impossible to disconnect, no server connected")
                }
            }
        })
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?

): View? {

// Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_client, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val serverURI   = arguments?.getString(MQTT_SERVER_URI_KEY)
        val clientId    = arguments?.getString(MQTT_CLIENT_ID_KEY)
        val username    = arguments?.getString(MQTT_USERNAME_KEY)
        val pwd         = arguments?.getString(MQTT_PWD_KEY)
        val cameraip    = arguments?.getString(MQTT_CAMERAIP_KEY)

        // Check if passed arguments are valid
        val camerasite = StringBuilder().append("").append("http://").append(cameraip).append("/").append(username).append("/1").toString()
        val webView = view.findViewById<View>(R.id.webView) as WebView
        webView.loadUrl(camerasite)
       if (

           serverURI   != null    &&
            clientId    != null    &&
            username    != null    &&
            pwd         != null        ) {

           Log.d(this.javaClass.name, "TRYING!!!!!!!!!!!!!!!!!!!")
            // Open MQTT Broker communication
           mqttClient = MQTTClient(context, serverURI, clientId)

           Log.d(this.javaClass.name, "FLAG!!!!!!!!!!!!!!!!!!!")
              
          // Get arguments passed by ConnectFragment


           Log.d("serverURI",serverURI)
           Log.d("clientID",clientId)
           Log.d("username",username)
           Log.d("pwd",pwd)
           Log.d("camera",camerasite)
// Connect and login to MQTT Broker
           mqttClient.connect( username,
                pwd,

                object : IMqttActionListener {

 override fun onSuccess(asyncActionToken: IMqttToken?) {

//THIS IS WHERE I HAVE DETERMINED THE PENDINGINTENT NEEDS TO GO!

}

                    override fun onFailure(asyncActionToken: IMqttToken?, exception: Throwable?) {
                        Log.d(this.javaClass.name, "Connection failure: ${exception.toString()}")
}
},

                object : MqttCallback {


                    override fun messageArrived(topic: String?, message: MqttMessage?) {
 }

                    override fun connectionLost(cause: Throwable?) {
                        Log.d(this.javaClass.name, "Connection lost ${cause.toString()}")
                    }

                    override fun deliveryComplete(token: IMqttDeliveryToken?) {

                        Log.d(this.javaClass.name, "Delivery complete")
                    }
                })
}
android mqtt alarmmanager android-pendingintent
1个回答
0
投票

我在我的几个项目中多次遇到这个问题。

然后我找到了解决方案。

第1步:转到Build.Gradle(项目)级别文件

第 2 步:向下选择任意线路。

第3步:按3 - 4次回车,(意思是在文件中仅添加空白行)

第 4 步:运行项目,您就得到了解决方案。

                  OR

当您收到错误时,您还获取缓存文件的文件路径

复制它并粘贴到文件资源管理器中

删除 ZIP 文件。然后再次运行应用程序,错误结束。

☻♥ 完成保留代码。

© www.soinside.com 2019 - 2024. All rights reserved.