我正在尝试从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.
那么我该如何解决这个问题呢?
1条答案
按热度按时间vawmfj5a1#
您可以使用
Serializable
来传递对象,如-〉现在通过这个商店类
现在从另一个活动接收此购物类
也可以使用
Parcelable
代替Serializable