我曾经使用MediaStore.Images.Media.insertImage但insertImage方法保存图像现在已弃用。该文件说:
此方法在 API 级别 29 中已弃用。插入图像应使用MediaColumns#IS_PENDING执行,它提供了对生命周期的更丰富的控制。
我真的不明白,因为MediaColumns.IS_PENDING它只是一个标志,我应该如何使用它?
我应该使用ContentValues吗?
如何使用导航抽屉设置导航组件?我如何在我的应用程序中使用它?
一个活动可以完成所有事情吗?
如何仅使用一个具有动态工具栏可见性的 Activity 和片段来处理工具栏可见性。此外,我需要关闭抽屉并使其无法访问的碎片。
这个问题是一个自我回答的问题,它更像是一个教程而不是现实生活中的 QA。
我很好奇存储库在MVVM架构中的作用。如果您决定将存储库添加到您的项目中,这个存储库是否只负责来自数据库或网络的数据?问题是关于SharedPreferencesor Files,我应该让存储库对此负责,还是应该将它们保留在ViewModel.
我正在 Android 中进行一些 Espresso 测试。测试失败并出现此错误:
java.lang.ClassCastException: androidx.fragment.app.testing.FragmentScenario$EmptyFragmentActivity 无法转换为 com.stavro_xhardha.pockettreasure.MainActivity
这是我的测试方法:
@Test
fun toolbarTitle_shouldContainCorrectInput() {
val mockNavController = mock(NavController::class.java)
val fragmentScenario = launchFragmentInContainer<SetupFragment>()
fragmentScenario.onFragment {
Navigation.setViewNavController(it.view!! , mockNavController)
}
onView(withId(R.id.toolbar)).check(matches(withText("Pick your country")))
}
Run Code Online (Sandbox Code Playgroud)
但是错误不是来自 Test 类,而是来自我的 Fragment under test 。崩溃是在这行代码中执行的:
override fun onStart() {
super.onStart()
(activity!! as MainActivity).supportActionBar?.hide() //here
}
Run Code Online (Sandbox Code Playgroud)
我不明白的是,当我在没有测试的情况下正常运行应用程序时,我没有遇到任何错误。
android android-fragments android-testing android-navigation android-espresso
根据Coils 文档,我不必为我的图像进行任何配置fit()。问题是,ImageView没有正确加载:
这是我的ImageViewwith配置Picasso:
picasso.load(unsplashResult?.photoUrls?.thumbnailUrl)
.fit()
.error(R.drawable.img_placeholder)
.placeholder(R.drawable.img_placeholder)
.into(currentImageView)
Run Code Online (Sandbox Code Playgroud)
这是代码Coil:
currentImageView.load(unsplashResult?.photoUrls?.thumbnailUrl) {
placeholder(R.drawable.img_placeholder)
error(R.drawable.img_placeholder)
}
Run Code Online (Sandbox Code Playgroud)
但我的结果ImageView不一样。
这是Coil加载它们的方式:
这就是Picasso加载它们的方式:
问题是,我怎样才能达到相同的结果Coil?
编辑
这是我的ImageView:
<ImageView
android:adjustViewBounds="true"
android:id="@+id/ivUnsplashImage"
android:src="@drawable/some_unsplash_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud) 您如何Http使用Retrofit和正确测试请求/响应Coroutines?
我正在使用最新Retrofit版本:2.6.0它支持suspend功能。
编辑
我还提供了一些易于实现的代码:
接口
@GET
suspend fun getCountriesListAsync(@Url url: String): Response<ArrayList<Country>>
Run Code Online (Sandbox Code Playgroud)
一些存储库
suspend fun makeCountryApiCallAsync(): Response<ArrayList<Country>> =
treasureApi.getCountriesListAsync(COUNTRIES_API_URL)
Run Code Online (Sandbox Code Playgroud)
实施ViewModel:
private suspend fun makeCountriesApiCall() {
withContext(Dispatchers.Main) {
switchProgressBarOn()
}
try {
val countriesListResponse = setupRepository.makeCountryApiCallAsync()
if (countriesListResponse.isSuccessful) {
withContext(Dispatchers.Main) {
switchProgressOff()
countriesList.value = countriesListResponse.body()
}
} else {
withContext(Dispatchers.Main) {
showErrorLayout()
}
}
} catch (e: Exception) {
e.printStackTrace()
withContext(Dispatchers.Main) {
showErrorLayout()
}
}
}
Run Code Online (Sandbox Code Playgroud) 我必须执行此用例(不是代码,只是正确的用法) 用例:我需要每天在00:30从网络中获取一些数据。这些数据为我提供了一些特定的时间,其中之一大约是4:30(每天以+1分钟-1分钟的时间变化,取决于服务器的响应,不能在任何地方使用++或-逻辑)。在这一点(4:30),我需要安排一个Alarm。尚不清楚:
我应该直接使用AlarmManager吗?
我应该使用WorkManager来获取需要报警的时间,而不是使用AlarmManager吗?
我应该只使用WorkManager吗?
我感到困惑的原因是,我读过的一些博客说,如果我在特定时间做一些工作,最好坚持使用AlarmManager,但仍然可以使用WorkManager来做到这一点。
那怎么办呢?
假设有一个Dao具有以下两种方法的类:
1)
delete(items: List<Item>): Completable
Run Code Online (Sandbox Code Playgroud)
2)
insert(items: List< Item >): Single<List<Long>>
Run Code Online (Sandbox Code Playgroud)
我怎样才能把它们连成一个@transaction在方法Dao开始与“删除方法”类,然后返回“插入方法”结果呢?
我想要一个带有这样签名的方法:
@Transaction
fun deleteAndInsert(): Single<List<Long> > {
...
}
Run Code Online (Sandbox Code Playgroud) 我想知道如何处理BroadcastReceiver. 我必须运行一些suspending 方法,目前我正在使用 aGlobalScope.launch或 a runBlocking。有没有另一种方法来控制正在被解雇的工作并在BroadcastReceiver()完成时取消?这是一个AlarmManager专门的。
对于所有说切换到WorkManager答案的人来说,答案是否定的,因为我是在准确的时间安排工作,而WorkManager不是为你做。因此,为了设置,Alarms我必须suspend在AlarmManager触发后从方法中读取一些数据。我也试过这个解决方案:
//inside Alarm Managers onReceive Method
val job = coroutineScope.launch {
delayingOperationMethod()
}
job.invokeOnCompletion {
coroutineScope.cancel()
}
Run Code Online (Sandbox Code Playgroud)
哪里job只是:
private val job: Job = Job()
private val coroutineScope = CoroutineScope(job + Dispatchers.IO)
Run Code Online (Sandbox Code Playgroud)
这是一种方法吗?
android ×8
alarmmanager ×1
android-architecture-navigation ×1
android-mvvm ×1
android-room ×1
coil ×1
dart ×1
flutter ×1
mediastore ×1
retrofit2 ×1
rx-java2 ×1