我目前正在使用databinding和MVVM architectureAndroid.在ViewModel中获取字符串资源的最佳方法是什么.
我没有使用新AndroidViewModel组件,eventbus或RxJava
我正在经历接触的方式,其中Activity将负责提供资源.但最近我在这个答案中找到了一个类似的问题,其中使用应用程序上下文的单个类提供了所有资源.
哪种方法更好?或者还有其他我可以尝试的东西吗?
我试图在NEXUS 9中做广告并得到ADVERTISE_FAILED_DATA_TOO_LARGE的错误.在成功通告后添加服务时,它工作得非常好但是如果我通过"广告数据"构建器添加服务以便其他设备可以在扫描时进行过滤,则会收到错误代码1,即ADVERTISE_FAILED_DATA_TOO_LARGE
a)工作准则
public void startAdvertisingService() {
AdvertiseSettings settings = new AdvertiseSettings.Builder()
.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
.setTimeout(0)
.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY)
.build();
AdvertiseData.Builder advertiseData = new AdvertiseData.Builder();
advertiseData.setIncludeDeviceName(true);
BluetoothLeAdvertiser myBluetoothLeAdvertiser = btAdapter.getBluetoothLeAdvertiser();
myBluetoothLeAdvertiser.stopAdvertising(mAdvertiseCallback);
myBluetoothLeAdvertiser.startAdvertising(settings, advertiseData.build(),mAdvertiseCallback);
}
private AdvertiseCallback mAdvertiseCallback = new AdvertiseCallback() {
@Override
public void onStartSuccess(AdvertiseSettings settingsInEffect) {
super.onStartSuccess(settingsInEffect);
BLEBroadcast();
}
@Override
public void onStartFailure(int errorCode) {
String description = "";
if (errorCode == AdvertiseCallback.ADVERTISE_FAILED_FEATURE_UNSUPPORTED)
description = "ADVERTISE_FAILED_FEATURE_UNSUPPORTED";
else if (errorCode == AdvertiseCallback.ADVERTISE_FAILED_TOO_MANY_ADVERTISERS)
description = "ADVERTISE_FAILED_TOO_MANY_ADVERTISERS";
else if (errorCode …Run Code Online (Sandbox Code Playgroud) peripherals android bluetooth bluetooth-lowenergy cbperipheral
我正在尝试使用重新安装Android应用程序登录facebook.最初我使用Google注册并成功将其与firebase链接.
但是,当我尝试使用Facebook时,它会给出一个
FirebaseAuthUserCollisionException
Run Code Online (Sandbox Code Playgroud)
我在Firebase文档中读到了您可以这样做的内容
FirebaseUser prevUser = currentUser;
currentUser = auth.signInWithCredential(credential).await().getUser();
// Merge prevUser and currentUser accounts and data
// ...
Run Code Online (Sandbox Code Playgroud)
但这里await()方法不再存在.在搜索了一下后,我发现了这个解决方案
Tasks.await(mAuth.signInWithCredential(credential)).getUser();
Run Code Online (Sandbox Code Playgroud)
但是当获取已经链接的当前用户时,这也会出错.我该怎么做才能解决这个问题?
我正在进行单元测试
@RunWith(MockitoJUnitRunner.class)
我使用firebase Analytics来记录事件
MyApplication.getAnalytics().getInstance(appContext).logEvent(eventType,bundle)
这在我的Application类中
public static FirebaseAnalytics getAnalytics() {
return FirebaseAnalytics.getInstance(appContext);
}
Run Code Online (Sandbox Code Playgroud)
现在,在运行测试时,我正在接受NullPointerException.为我的单元测试初始化Analytics或者忽略它们的正确方法是什么.
我没有得到上下文,以防我尝试在我的测试设置方法中初始化它.
我想在Jacaco测试覆盖率报告中排除一些源文件.对于其他生成的代码,我这样做:
classDirectories = fileTree(
dir: "${project.buildDir}/intermediates/classes/debug/com",
excludes: [
'**/R.class',
'**/R$*.class']
)
Run Code Online (Sandbox Code Playgroud)
但是当我尝试这样做时,为了排除Java文件:
dir: "${project.buildDir}/intermediates/classes/debug/com",
excludes: [
'src//java/com/example/application/Constants.java']
Run Code Online (Sandbox Code Playgroud)
也尝试过这样:. '**/application/Constants.class'它不起作用.我需要在这里包括路径: dir: "${project.buildDir}/intermediates/classes/debug/com"?
我正在使用Android studio 3.0(我认为这不重要).我正在尝试的完整代码:
task jacocoTestReport(type: JacocoReport) {
group = "Reporting"
description = "Generate Jacoco coverage reports"
reports {
xml.enabled = true
html.enabled = true
}
sourceDirectories = files(sourceSets)
classDirectories = fileTree(
dir: "${project.buildDir}/intermediates/classes/debug/com",
excludes: [
'src//java/com/example/application/Constants.java', //this is not working
'**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*$ViewInjector*.*',
'**/*$ViewBinder*.*',
'**/*$Lambda$*.*', // Jacoco can not handle several "$" in class name. …Run Code Online (Sandbox Code Playgroud) 我将一个JSON文件作为类的输入,并使用 gson 通过相应的数据类解析值。
我想调用一个以String值作为参数的函数。
允许的字符串值由从文件解析的值决定JSON。我可以以某种方式检查在编译时传递给函数的字符串值并在编译时给出错误吗?
或者如果我可以根据 JSON 中的值在函数的参数中只允许某些值
用例详细说明:
我正在构建一个 SDK,其中使用 sdk 的人输入 json 字符串。json 是标准化的,并在我的代码中进行解析。
{
"name": "Test",
"objects": [
{
"name": "object1",
"type": "object1"
}
]
}
Run Code Online (Sandbox Code Playgroud)
这里的名称值和其他值可能会根据使用它的开发人员的输入而变化,但密钥保持不变。但是我们需要使用对象名称参数中的值来调用函数。
fun testMethod(objectName:String)
Run Code Online (Sandbox Code Playgroud)
因此开发人员将 testMethod 称为testMethod(object1)。
我需要object1基于 json 验证参数,但是有什么办法可以将测试方法参数限制为object1仅在开发人员调用时在编译时给出错误testMethod(obj1)
现在我解析 JSON 并在里面进行检查testMethod()
android ×7
java ×2
bluetooth ×1
cbperipheral ×1
device-owner ×1
facebook ×1
firebase ×1
jacoco ×1
json ×1
kiosk-mode ×1
kotlin ×1
mockito ×1
mvvm ×1
nfc ×1
peripherals ×1
unit-testing ×1
updates ×1