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



我正在尝试从NotificationListiner服务启动一个UI活动,然后在UI中当用户点击yes时:执行pendingIntent((;else:finish((

我试图用Dialog,AlertDialog实现上述功能,但由于NotificationListiner是一个后台服务,所以它不起作用。然后制作了新的CCD_ 3;

尝试使用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.

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

您可以使用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

相关内容

  • 没有找到相关文章

最新更新