尝试将一些图像上传到AWS的s3存储桶中。
在API级别26以下可以正常工作,但显示错误
无法执行HTTP请求:API级别26及更高版本的超时。

二手sdk:

implementation 'com.amazonaws:aws-android-sdk-s3:2.13.+'
implementation ('com.amazonaws:aws-android-sdk-mobile-client:2.13.+@aar') { transitive = true }
implementation ('com.amazonaws:aws-android-sdk-auth-userpools:2.13.+@aar') { transitive = true }

我已经从Application开始服务,例如:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channel = NotificationChannel(AWS_NOTIFICATION_CHANNEL_ID, AWS_NOTIFICATION_CHANNEL_ID,
                NotificationManager.IMPORTANCE_HIGH)
        (getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).createNotificationChannel(channel)
        val notification = Notification.Builder(applicationContext, AWS_NOTIFICATION_CHANNEL_ID)
                .setContentTitle(AWS_NOTIFICATION_TITLE)
                .setContentText(AWS_NOTIFICATION_CONTENT)
                .setSmallIcon(R.mipmap.sym_def_app_icon)
                .build()
        val tsIntent = Intent(applicationContext, TransferService::class.java)
        tsIntent.putExtra(TransferService.INTENT_KEY_NOTIFICATION, notification)
        tsIntent.putExtra(TransferService.INTENT_KEY_NOTIFICATION_ID, AWS_NOTIFICATION_ID)
        tsIntent.putExtra(TransferService.INTENT_KEY_REMOVE_NOTIFICATION, true)
        applicationContext.startForegroundService(tsIntent)
    } else {
        applicationContext.startService(Intent(applicationContext, TransferService::class.java))
    }

使用或不使用 TransferService 欣赏两种解决方案。

最佳答案

为了在上支持 TransferService ,奥利奥及更高版本需要启动 ForegroundService

2.11.0 中引入了 TransferNetworkLossHandler ,它是侦听网络连接更改的广播接收器。 TransferNetworkLossHandler 在网络脱机时暂停正在进行的传输,并在网络重新联机时恢复暂停的传输。 TransferService 在创建服务时注册 TransferNetworkLossHandler ,并在销毁服务时注销处理程序。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    val channel = NotificationChannel(AWS_NOTIFICATION_CHANNEL_ID,
    AWS_NOTIFICATION_CHANNEL_ID,
    NotificationManager.IMPORTANCE_LOW)

    (getSystemService(Context.NOTIFICATION_SERVICE) as
    NotificationManager).createNotificationChannel(channel)

    val notification = Notification.Builder(applicationContext,
    AWS_NOTIFICATION_CHANNEL_ID)
         .setContentTitle(AWS_NOTIFICATION_TITLE)
         .setContentText(AWS_NOTIFICATION_CONTENT)
         .setSmallIcon(R.mipmap.sym_def_app_icon)
         .setAutoCancel(true)

    val tsIntent = Intent(applicationContext, TransferService::class.java)
    tsIntent.putExtra(TransferService.INTENT_KEY_NOTIFICATION, notification.build())
    tsIntent.putExtra(TransferService.INTENT_KEY_NOTIFICATION_ID,AWS_NOTIFICATION_ID)
    tsIntent.putExtra(TransferService.INTENT_KEY_REMOVE_NOTIFICATION, true)

    applicationContext.startForegroundService(tsIntent)
} else {
   applicationContext.startService(Intent(applicationContext,
   TransferService::class.java))
}

有关更多详细信息,请参见:aws-storage

10-06 06:51