I am trying to put setTapAction
of my complication to call the activity, but it’s not working whenever I tap it does not do anything just a ripple effect, my code is below.
class CustomComplication : SuspendingComplicationDataSourceService() {
// Retrieves your data, in this case, we grab an incrementing number from Datastore.
val prefs = getSharedPreferences("MY_PREFS_NAME", MODE_PRIVATE)
val tempF = prefs.getString("tempF", "") //"No name defined" is the default value.
val intent = Intent(this, MainActivity::class.java).apply {
component = ComponentName(this@CustomComplication, MainActivity::class.java)
}
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
override fun getPreviewData(type: ComplicationType): ComplicationData? {
if (type != ComplicationType.SHORT_TEXT) {
return null
}
return createComplicationData(tempF.toString(), "Monday")
}
override suspend fun onComplicationRequest(request: ComplicationRequest): ComplicationData? {
Log.d("BABT", "onComplicationRequest() id: ${request.complicationInstanceId}")
return when (request.complicationType) {
ComplicationType.SHORT_TEXT -> ShortTextComplicationData.Builder(
text = PlainComplicationText.Builder(text = tempF.toString()).build(),
contentDescription = PlainComplicationText
.Builder(text = "Short Text version of Number.").build(),
).setMonochromaticImage(
MonochromaticImage.Builder(
image = Icon.createWithResource(this, R.drawable.iconforbody),
).build(),
)
.setTapAction(pendingIntent)
.build()
else -> {
if (Log.isLoggable("BABT", Log.WARN)) {
Log.w("BABT", "Unexpected complication type ${request.complicationType}")
}
null
}
}
}
private fun createComplicationData(text: String, contentDescription: String) =
ShortTextComplicationData.Builder(
text = PlainComplicationText.Builder(text).build(),
contentDescription = PlainComplicationText.Builder(contentDescription).build()
).build()
}
I tried above code but it is not working
Starting in Android 12, you have to use PendingIntent#FLAG_IMMUTABLE
or PendingIntent#FLAG_MUTABLE
, it's strongly recommended to use PendingIntent#FLAG_IMMUTABLE
:
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)