小编Kin*_*ing的帖子

Android Kotlin - 将 jsonArray 转换为 Array<String>

我从 API 得到一个数组:

val pics = response.getJSONArray("pics")
Run Code Online (Sandbox Code Playgroud)

pics 包含像:

["https:\/\/www.bla.com\/extern\/v\/pics\/mitte\/19\/55\/497173801955.jpg","https:\/\/www.bla.com\/extern\/v\/pics\/mitte\/10\/34\/242830811034.jpg","https:\/\/www.bla.com\/extern\/v\/pics\/mitte\/86\/23\/808728238623.jpg","https:\/\/www.bla.com\/extern\/v\/pics\/mitte\/90\/41\/146747399041.jpg","https:\/\/www.bla.com\/extern\/v\/pics\/mitte\/47\/41\/672475854741.jpg","https:\/\/www.bla.com\/extern\/v\/pics\/mitte\/63\/94\/771076926394.jpg","https:\/\/www.bla.com\/extern\/v\/pics\/mitte\/36\/42\/182330463642.jpg","https:\/\/www.bla.com\/extern\/v\/pics\/mitte\/29\/96\/948397532996.jpg","https:\/\/www.bla.com\/extern\/v\/pics\/mitte\/82\/54\/761385508254.jpg","https:\/\/www.bla.com\/extern\/v\/pics\/mitte\/41\/42\/142837364142.jpg","https:\/\/www.bla.com\/extern\/v\/pics\/mitte\/66\/25\/215324906625.jpg"]
Run Code Online (Sandbox Code Playgroud)

我需要把它变成一个 ArrayList<String>

这是将 JAVA 解决方案转换为 kotlin 后的多次尝试之一:

val list = ArrayList<String>()
for (i in 0 until pics.length()) {
   list.add(pics.getJSONObject(i).getString("name"))
}
Run Code Online (Sandbox Code Playgroud)

然后当我尝试list在 ViewPager 中使用时,我得到类型不匹配:

Required: Array<String>

Found:kotlin.collections.ArrayList<String
Run Code Online (Sandbox Code Playgroud)

这该怎么做??

为什么不能像 PHP smh 一样只是简单的 array() 这一切都太烦人了

android json kotlin

2
推荐指数
1
解决办法
7404
查看次数

Android Kotlin/JAVA - 自1970年以来以秒为单位获取当前世界时间/ UTC

正如标题所说,我需要自1970年以来的当前工作时间.

这是我到目前为止所尝试的:

System.currentTimeMillis()
Run Code Online (Sandbox Code Playgroud)

val df = DateFormat.getTimeInstance()
df.setTimeZone(TimeZone.getTimeZone("gmt"))
val gmtTime = df.format(Date())
Run Code Online (Sandbox Code Playgroud)

但两者都显示基于手机时间的时间,因此当用户手动更改手机时间时,它将受到影响

有没有办法在不使用extern API的情况下在几秒钟内获得当前的工作时间?

java android timestamp utc kotlin

2
推荐指数
1
解决办法
652
查看次数

Android kotlin google play billing onPurchasesUpdated 不覆盖任何内容或从未使用过

我正在按照此文档来实施 google pay:

https://developer.android.com/google/play/billing/billing_library_overview

运行此函数并购买产品后:

fun buy() {

    val skuList = listOf("vip_small")

    if (billingClient.isReady) {
        val params = SkuDetailsParams
                .newBuilder()
                .setSkusList(skuList)
                .setType(BillingClient.SkuType.INAPP)
                .build()
        billingClient.querySkuDetailsAsync(params) { responseCode, skuDetailsList ->

            if (responseCode == BillingClient.BillingResponse.OK) {

                Log.d("lets", "querySkuDetailsAsync, responseCode: $responseCode")


                val flowParams = BillingFlowParams.newBuilder()
                        .setSku("vip_small")
                        .setType(BillingClient.SkuType.INAPP) // SkuType.SUB for subscription
                        .build()

                val responseCodee = billingClient.launchBillingFlow(this, flowParams)

                Log.d("lets", "launchBillingFlow responseCode: $responseCodee")

            } else {
                Log.d("lets", "Can't querySkuDetailsAsync, responseCode: $responseCode")
            }
        }
    } else {
        Log.d("lets", "Billing Client not ready")
    }
}
Run Code Online (Sandbox Code Playgroud)

效果很好,我想知道是否已购买,所以我从鳕鱼中添加了以下代码:

override fun …
Run Code Online (Sandbox Code Playgroud)

java android billing kotlin

2
推荐指数
1
解决办法
1263
查看次数

Android FCM 定义频道 ID 并通过 PHP 向该频道发送通知

我对此完全迷失了,打开了 20 个标签来解决这个烦人的难题。我已经阅读了多个教程,但没有一个回答在哪里定义频道 ID 以及如何通过 PHP 向它发送通知。

override fun onMessageReceived(remoteMessage: RemoteMessage?) {
    showNotification(remoteMessage!!.data["message"]!!)
}

private fun showNotification(message: String) {

    val i = Intent(this, MainActivity::class.java)
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

    val pendingIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT)


    val builder = NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentTitle("FCM Test")
            .setContentText(message)
            .setLargeIcon(BitmapFactory.decodeResource(resources, com.amoflirt.amoflirt.R.drawable.ic_vip_heart))
            .setSmallIcon(com.amoflirt.amoflirt.R.drawable.common_google_signin_btn_icon_dark)
            .setContentIntent(pendingIntent)


    val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

    manager.notify(0, builder.build())
}
Run Code Online (Sandbox Code Playgroud)

无论我在这里更改什么,它都不会影响发送/接收通知的方式,就像 setLargeIcon 没有添加任何内容一样。

这是PHP:

define( 'API_ACCESS_KEY', 'xxx');

 $msg = array(
            'body'  => $body,
            'title' => $title,
            );

$fields = array(
                'to' => $token,
                'notification'  => $msg
                ); …
Run Code Online (Sandbox Code Playgroud)

php android push-notification

1
推荐指数
1
解决办法
2395
查看次数

标签 统计

android ×4

kotlin ×3

java ×2

billing ×1

json ×1

php ×1

push-notification ×1

timestamp ×1

utc ×1