Android AWS TransferService在API级别26及更高版本中不起作用

时间:2019-05-23 06:07:46

标签: android amazon-s3 kotlin aws-sdk

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

使用的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 的情况下,都赞赏这两种解决方案。

1 个答案:

答案 0 :(得分:0)

最后,我找到了答案

要在 Oreo 及更高版本上支持 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