gson 如何在Intent.putExtra中传递pendingInent;从另一个活动启动pendingIntent

w7t8yxp5  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(201)

我正在尝试从NotificationListiner服务启动UI活动,然后在UI中,当用户单击“是”时:执行pendingIntent(); else:完成()
我试着用Dialog,AlertDialog来实现上面的功能,但是因为NotificationListiner是后台服务,所以它不起作用。
已尝试使用Gson
SbnContainer.kt

class SbnContainer {
    lateinit var notification: Notification
    lateinit var title: String
    lateinit var desc: String
    constructor(){
        Log.d("TAG", "Salaam")
    }
    constructor(notification: Notification, title:String, desc:String){
        this.notification = notification
        this.title= title
        this.desc = desc
    }
}

NotificationService呼叫

var i = Intent(this,CAlertWindow::class.java)
    i.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    val gson = Gson()
    var a = SbnContainer(sbn.notification, title, desc)
    val myJson = gson.toJson(a)
    i.putExtra("PACKAGE", myJson)
    startActivity(i)

CAlertWindow(AppCompatActivity)接收

val gson = Gson()
        val ob: SbnContainer = gson.fromJson(intent.getStringExtra("PACKAGE"), SbnContainer::class.java)
        pendingIntent = ob.notification.contentIntent
        alertWindowLayoutBinding.alrTitle.text = ob.title
        alertWindowLayoutBinding.alrDesc.text = ob.desc
        Log.d("Abdx79/AlertWindow", "In Alert Window")
        alertWindowLayoutBinding.alrOpnB.setOnClickListener {
            pendingIntent.send()
        }
        alertWindowLayoutBinding.alrDntB.setOnClickListener {
            finish()
        }

调用startActivity(i)时出错

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abdx79.notifications_manager/com.abdx79.notifications_manager.CAlertWindow}: java.lang.RuntimeException: Unable to invoke no-args constructor for interface java.lang.CharSequence. Registering an InstanceCreator with Gson for this type may fix this problem.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3430)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
        at android.os.Handler.dispatchMessage(Handler.java:112)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7625)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
     Caused by: java.lang.RuntimeException: Unable to invoke no-args constructor for interface java.lang.CharSequence. Registering an InstanceCreator with Gson for this type may fix this problem.

那么我该如何解决这个问题呢?

vawmfj5a

vawmfj5a1#

您可以使用Serializable来传递对象,如-〉

class Shop(
            val name: String,
            val details: String?,
            val id: Int,
            ):Serializable

现在通过这个商店类

var shop = Shop("name","details",1)
    val intent = Intent(this, AnotherActivity::class.java).putExtra(
                "SHOP",
                shop 
            )
    startActivity(intent)

现在从另一个活动接收此购物类

val shope = intent.getSerializableExtra("SHOP") as Shop

也可以使用Parcelable代替Serializable

相关问题