gpt4 book ai didi

Android AWS TransferService 不适用于 API 级别 26 及更高级别

转载 作者:行者123 更新时间:2023-11-29 02:20:27 24 4
gpt4 key购买 nike

尝试将一些图像上传到 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 }

我已经从应用程序开始服务,例如:

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 的两种解决方案。

最佳答案

要在Oreo及以上支持TransferService需要启动ForegroundService

TransferNetworkLossHandler,在 2.11.0 中引入了一个监听网络连接变化的广播接收器。 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

关于Android AWS TransferService 不适用于 API 级别 26 及更高级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56268970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com