小编P K*_*ers的帖子

如何在 network_security_config.xml 中包含 BuildConfig 或资源

为了在 Android 中的本地主机上进行 http-API 调用,我需要添加一个网络安全配置来定义我的本地 IP 地址。当然,每个开发者的本地IP地址都是不同的,所以我把这个值放进去,gradle.properties这样每个人都可以自己覆盖它。但是,我还没有找到如何将该值包含在文件中network-security-config.xml。我尝试创建 abuildConfigFieldresValuein build.gradle,但两者似乎都不起作用。我能做些什么?

网络安全配置.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <!-- We are allowed to call localhost on http instead of https:
        /sf/ask/3215860301/ -->
        <domain includeSubdomains="true">LOCALHOST</domain>
    </domain-config>
</network-security-config>
Run Code Online (Sandbox Code Playgroud)

应用程序/build.gradle

buildTypes {
    debug {
        buildConfigField "String", "LOCALHOST", localIpAddress
        resValue "string", "LOCALHOST", localIpAddress
    }
}
Run Code Online (Sandbox Code Playgroud)

gradle.properties

localIpAddress="IP_ADDRESS_HERE"
Run Code Online (Sandbox Code Playgroud)

network_security_config.xml也欢迎任何其他如何优雅地包含我的本地 IP 地址的解决方案!

android localhost android-build-type android-network-security-config

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

活动导航:使用popEnter和popExit类似片段的自定义动画

使用以下代码可以使用动画更改活动:

Bundle animation = ActivityOptions.makeCustomAnimation(App.getContext(), R.anim.enter_from_right, R.anim.exit_to_left).toBundle();
startActivity(intent, animation);
Run Code Online (Sandbox Code Playgroud)

对于片段,您可以在FragmentTransaction上执行类似的操作:

// ...
transaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left);
// ...
Run Code Online (Sandbox Code Playgroud)

这有效!但是我想在按下时弹出一个动画(从后台弹出).对于片段,您只需添加2个动画资源(popEnter和popExit):

transaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right);
Run Code Online (Sandbox Code Playgroud)

如何为活动创建相同的"背景动画"?

navigation android-animation android-activity onbackpressed

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

自“androidx-livedata”更新 2.3.0 以来,lint 分析期间出现意外失败

运行 lint 时发生了一个奇怪的错误。这似乎发生在androidx.lifecycle从 2.2.0 更新到 2.3.0

\n
../../src/main/java/my/project/MyService.kt: Unexpected failure during lint analysis of MyService.kt\n(this is a bug in lint or one of the libraries it depends on)\n\nMessage: org.jetbrains.uast.UastErrorType cannot be cast to com.intellij.psi.PsiClassType\n\nThe crash seems to involve the detector androidx.lifecycle.lint.NonNullableMutableLiveDataDetector.\nYou can try disabling it with something like this:\n    android {\n        lintOptions {\n            disable "NullSafeMutableLiveData"\n        }\n    }\n\nStack: ClassCastException:NonNullableMutableLiveDataDetector.visitMethodCall(NonNullableMutableLiveDataDetector.kt:103)\n\xe2\x86\x90UElementVisitor$DelegatingPsiVisitor.visitMethodCallExpression(UElementVisitor.kt:1079)\n\xe2\x86\x90UElementVisitor$DelegatingPsiVisitor.visitCallExpression(UElementVisitor.kt:1059)\n\xe2\x86\x90UCallExpression$DefaultImpls.accept(UCallExpression.kt:85)\n\xe2\x86\x90UCallExpressionEx$DefaultImpls.accept(UCallExpression.kt:-1)\n\xe2\x86\x90KotlinUSimpleReferenceExpression$KotlinAccessorCallExpression.accept(KotlinUSimpleReferenceExpression.kt:129)\n\xe2\x86\x90KotlinUSimpleReferenceExpression.visitAccessorCalls(KotlinUSimpleReferenceExpression.kt:116)\n\xe2\x86\x90KotlinUSimpleReferenceExpression.accept(KotlinUSimpleReferenceExpression.kt:83)\n\nYou can set environment variable LINT_PRINT_STACKTRACE=true to dump a full stacktrace to stdout.\nThis issue type represents a problem running lint itself. …
Run Code Online (Sandbox Code Playgroud)

android lint mutablelivedata androidx

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

在 Kotlin 的一行中使用(自定义)项目初始化新的数组列表

在 Kotlin 中,我创建了一个扩展函数来初始化一个带有自定义项的新 ArrayList,如下所示:

fun <T> arrayListFrom(vararg item: T): ArrayList<T> {
    return item.toMutableList() as ArrayList<T>
}
Run Code Online (Sandbox Code Playgroud)

通过这种方式,我可以轻松地创建一个这样的数组列表

arrayListFrom(MyCustomItem(1), MyCustomItem(2))
Run Code Online (Sandbox Code Playgroud)

...无需创建一个新的空的,并将所有元素一一添加到其中

Kotlin 有这么多有用的集合函数,我无法想象我需要这个扩展来轻松初始化 arrayList,但找不到另一种简单的方法。我在这里错过了一些有用的 Kotlin 函数吗?

arraylist kotlin

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

Java日历时区更改未按预期工作

我正在使用Java日历在午夜(GMT)创建某个日期,然后将时区更改为我的本地时区,以确保请求的时间为1:00(GMT +1)。下面的代码有效,包括。2个成功断言。

TimeZone TIMEZONE_GMT = TimeZone.getTimeZone("GMT");
TimeZone TIMEZONE_LOCAL = TimeZone.getDefault(); // GMT + 1

private Calendar getMidnightGmtCalendarWithLocalTimezone() {
    Calendar calendar = Calendar.getInstance(Locale.GERMAN);
    calendar.set(2017, Calendar.JANUARY, 1, 0, 0, 0);

    calendar.setTimeZone(TIMEZONE_GMT); // Probably not required
    assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY)); // Assert 1

    // Log.i("TAG", "" + calendar.get(Calendar.HOUR_OF_DAY));

    calendar.setTimeZone(TIMEZONE_LOCAL);
    assertEquals(1, calendar.get(Calendar.HOUR_OF_DAY)); // Assert 2
    return calendar;
}
Run Code Online (Sandbox Code Playgroud)

现在,我删除第一个断言,并且我希望没有任何变化。现实:第二个断言现在失败了!我试图了解.get()的实现,而且显然它也需要一些时间,因此它不仅仅是收集值。仍然我不明白为什么我的第二个断言失败了。

当我取消对Log行的注释时,第二个断言再次成功(只是为了确保问题出自calendar.get()而不是断言!)

所以第一个问题:为什么会这样?第二问:当我将时区GMT设置为午夜时,如何确保我的日历实例具有本地时区?(换句话说,如何正确转换时区?)

编辑: 我在Android上使用此,所以不能使用Java8。Joda时间(如下建议)是一个很好的选择,我将对此进行深入研究。但是,作为我的问题的答案,我想知道它如何与Java7 Calendar一起使用,因为目前我已绑定到该代码。

timezone android calendar java-7

0
推荐指数
1
解决办法
1144
查看次数