小编use*_*924的帖子

android.text.format.Formatter.formatFileSize() 返回错误的格式

这里我比较了Android内置方法来格式化文件和自定义来自/sf/answers/391988971/

\n
Timber.d(\n    "Format file size using Formatter.formatFileSize " +\n            "${Formatter.formatFileSize(this, 1_000_000_000L)}, " +\n            "${Formatter.formatFileSize(this, 1_073_741_824L)}"\n)\n\nTimber.d(\n    "Format file size using readableFileSize " +\n            "${readableFileSize(1_000_000_000L)}, " +\n            "${readableFileSize(1_073_741_824L)}"\n)\n
Run Code Online (Sandbox Code Playgroud)\n

\n
fun readableFileSize(size: Long): String? {\n    if (size <= 0) return "0"\n    val units = arrayOf("B", "kB", "MB", "GB", "TB")\n    val digitGroups = (log10(size.toDouble()) / log10(1024.0)).toInt()\n    return DecimalFormat("#,##0.#").format(size / 1024.0.pow(digitGroups.toDouble()))\n        .toString() + " " + units[digitGroups]\n}\n
Run Code Online (Sandbox Code Playgroud)\n

这是结果:

\n
Format file size using Formatter.formatFileSize 1,00\xc2\xa0GB, 1,07\xc2\xa0GB\nFormat file size using readableFileSize …
Run Code Online (Sandbox Code Playgroud)

android

6
推荐指数
0
解决办法
792
查看次数

在 Android 29 上启用禁用 WiFi

public boolean WifiManager.setWifiEnabled (boolean enabled)
Run Code Online (Sandbox Code Playgroud)

此方法在 API 级别 29 中已弃用。从 Build.VERSION_CODES#Q 开始,不允许应用程序启用/禁用 Wi-Fi。兼容性说明:对于以 Build.VERSION_CODES.Q 或更高版本为目标的应用程序,此 API 将始终返回 false 且无效。如果应用面向较旧的 SDK(Build.VERSION_CODES.P 或更低版本),则它们可以继续使用此 API。

我们如何在 Android 29 上禁用 WiFi?

android wifimanager android-wifi

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

User Messaging Platform 2.0.0 版本崩溃

我更新自

implementation 'com.google.android.ump:user-messaging-platform:1.0.0'
Run Code Online (Sandbox Code Playgroud)

implementation 'com.google.android.ump:user-messaging-platform:2.0.0'
Run Code Online (Sandbox Code Playgroud)

现在我在 crashlytics 中收到了很多崩溃(这种情况只发生在欧洲的用户身上,这就是为什么我发布了但没有在我自己的设备上注意到):

Fatal Exception: java.lang.VerifyError
Verifier rejected class wa.f: java.lang.Object wa.f.h(java.lang.Object) failed to verify
...
com.google.android.gms.internal.consent_sdk.zzu.run (zzu.java:38)
com.google.android.gms.internal.ads.zzdvf.run$bridge (zzdvf.java:38)
Run Code Online (Sandbox Code Playgroud)

知道如何修复它吗?

也在这里https://groups.google.com/g/google-admob-ads-sdk/c/VPUtbIGgEYw

ps暂时降级到1.0.0版本...

android

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

ViewCompat.setOnApplyWindowInsetsListener() 更改系统导航栏的背景

应用程序通常有这样的导航栏:

在此输入图像描述

但是当我添加ViewCompat.setOnApplyWindowInsetsListener()

ViewCompat.setOnApplyWindowInsetsListener(window.decorView) { _, insets ->
    binding.showingKeyboard = insets.isVisible(WindowInsetsCompat.Type.ime())
    WindowInsetsCompat.CONSUMED
}
Run Code Online (Sandbox Code Playgroud)

或者

ViewCompat.setOnApplyWindowInsetsListener(window.decorView) { _, insets ->
    binding.showingKeyboard = insets.isVisible(WindowInsetsCompat.Type.ime())
    insets
}
Run Code Online (Sandbox Code Playgroud)

就变成这样了

在此输入图像描述

为什么会发生这种情况?我不希望这个方法改变任何东西。我只是想用它来检测虚拟键盘是否可见(没有其他好的方法可以做到这一点)

android windowinsets

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

检查应用程序是否可以在 Android 12+ 上在后台启动前台服务

在应用程序实际尝试执行并获取之前,有什么方法可以检查我的应用程序是否可以在 Android 12 上在后台启动前台服务ForegroundServiceStartNotAllowedException

某种方法检查至少满足一个条件https://developer.android.com/guide/components/foreground-services#background-start-restriction-exemptions

我在活动中将后台服务作为前台启动

try {
    context.bindService( // p.s. this context of application, it's bounded to app process instead of activity context
        getServiceIntent(context),
        object : ServiceConnection {
            override fun onServiceConnected(
                className: ComponentName,
                service: IBinder
            ) {
                // service is running in background
                startForegroundService(context)
                // service should be in foreground mode (at least it should be very soon)
                (service as LocalBinder).getService().initialize()
                context.unbindService(this)
                bindServiceInProgress.set(false)
            }

            override fun onServiceDisconnected(arg0: ComponentName) {
                bindServiceInProgress.set(false)
            }
        },
        AppCompatActivity.BIND_AUTO_CREATE
    )
} catch …
Run Code Online (Sandbox Code Playgroud)

android foreground-service android-12

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

Android 模拟器 ARM64 无法启动“AVD Pixel_6_Pro_API_33_ARM64 的模拟器进程已终止。” 但 x86_64 工作正常

我无法在 Windows 10 系统上启动 ARM64 模拟器,但 x86_64 工作正常。Intel HAXM 安装成功,当然虚拟化已启用

在此输入图像描述

可能是什么原因?

双核英特尔酷睿 i5-7300U

更新

从控制台运行模拟器会打印下一个错误:

PANIC: Avd's CPU Architecture 'arm64' is not supported by the QEMU2 emulator on x86_64 host

更新2

看来我们无法在 x86_64 系统上为 ARM64 模拟器使用更新的 Android API - /sf/answers/5237330801/

android android-virtual-device android-emulator android-studio avd-manager

5
推荐指数
0
解决办法
1966
查看次数

任务generateMetadataFileForReleasePublication使用任务androidSourcesJar的此输出,而不声明显式或隐式依赖项

以下命令无法发布我的 Android 库:

./gradlew :my_sdk:publish --stacktrace
Run Code Online (Sandbox Code Playgroud)

这是我看到的错误:

Reason: Task ':my_sdk:generateMetadataFileForReleasePublication' uses this output of task ':my_sdk:androidSourcesJar' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed.
Run Code Online (Sandbox Code Playgroud)

我的文件中与发布相关的内容build.gradle如下:

task androidSourcesJar(type: Jar) {
    archiveClassifier.set("sources")
    from android.sourceSets.main.java.srcDirs
}

afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                groupId mGroupId
                artifactId mArtifactId
                version mVersionName

                from components.release

                artifact androidSourcesJar

                pom {
                    name = mLibraryName
                    description = mLibraryDescription
                }
            }
        }
        repositories …
Run Code Online (Sandbox Code Playgroud)

android maven-plugin android-gradle-plugin

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

Camera2:设置光学稳定不起作用(OIS)

我有三星 S10,它具有视频稳定功能。使用系统默认的相机应用程序,我可以看到启用和未启用时的区别:首先,如果启用,则会出现一些缩放预览,其次,在设备移动期间会很明显。

在此输入图像描述

我尝试使用 Camera2 API、全高清和后置摄像头(与默认系统应用程序相同)在自己的应用程序中启用稳定性。

我测试了characteristics.get(CameraCharacteristics.CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES)仅返回CONTROL_VIDEO_STABILIZATION_MODE_OFF,因此不支持此操作。

characteristics.get(CameraCharacteristics.LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION)CameraMetadata.LENS_OPTICAL_STABILIZATION_MODE_ON

因此,据我所知,这正是启用视频稳定(光学)的选项,应该与默认系统应用程序中的相同。

但是,当我对相机捕获会话配置进行下一步时,它不会改变任何内容,没有缩放预览(与默认系统相机应用程序一样),并且在移动过程中没有变化,因此视频在我的应用程序中与它应该有的相同使用默认相机应用程序并禁用视频稳定功能

captureRequestBuilder.set(
    CaptureRequest.LENS_OPTICAL_STABILIZATION_MODE,
    CameraMetadata.LENS_OPTICAL_STABILIZATION_MODE_ON
)
Run Code Online (Sandbox Code Playgroud)

所以设置这个参数不会改变任何东西。

为什么视频稳定功能在默认系统相机应用程序中有效,但在我自己使用 Camera2 API 的应用程序中无效?

android android-camera android-camera2

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

使用 Pusher 库时缺少类 org.slf4j.impl.StaticLoggerBinder (引用自: void org.slf4j.LoggerFactory.bind() 和其他 3 个上下文)

启用 R8 ( isMinifyEnabled = true) 并使用"com.pusher.pusher-java-client:2.4.4"库时,我在发布版本中收到以下错误:

AGPBI: {"kind":"error","text":"运行 R8 时检测到缺少类。请添加缺少的类或应用在 project\app\build\outputs\mapping\devRelease\missing_rules 中生成的其他保留规则.txt。","sources":[{}]} AGPBI: {"kind":"error","text":"缺少类 org.slf4j.impl.StaticLoggerBinder (引用自: void org.slf4j.LoggerFactory. bind() 和其他 3 个上下文)","sources":[{}],"tool":"R8"} 运行 R8 时检测到缺少类。请添加缺少的类或应用在project\app\build\outputs\mapping\devRelease\missing_rules.txt 中生成的其他保留规则。

缺少类 org.slf4j.impl.StaticLoggerBinder (引用自:void org.slf4j.LoggerFactory.bind() 和其他 3 个上下文)

我该如何解决?有了isMinifyEnabled = false它就可以毫无问题地构建

android android-build android-r8

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

TextField KeyboardType.Number 和 KeyboardType.Decimal 都允许输入小数分隔符

所以我只需要允许在字段中输入数字。

我配置了TextField

keyboardOptions = KeyboardOptions.Default.copy(
    keyboardType = KeyboardType.Number
)
Run Code Online (Sandbox Code Playgroud)

但它仍然允许我输入小数分隔符(逗号和点)

所以我看不出KeyboardType.Number和之间有什么区别KeyboardType.Decimal,它们的工作原理完全相同......

数字

@Stable
public final val Number: KeyboardType
A keyboard type used to request an IME that is capable of inputting digits. IME may provide inputs other than digits but it is not guaranteed.
Run Code Online (Sandbox Code Playgroud)

十进制

@Stable
public final val Decimal: KeyboardType
A keyboard type used to request an IME that is capable of inputting decimals. IME should explicitly provide a decimal separator as input, …
Run Code Online (Sandbox Code Playgroud)

android android-jetpack-compose android-compose-textfield

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