Tag: android

RuntimeException处理最佳实践

RuntimeExceptions应该表示编程错误,并且当我的observables里面的东西抛出RuntimeException时,我希望我的应用程序崩溃。 做这个的最好方式是什么? 现在我正在考虑这个解决方案(这是Kotlin,但我希望这是可以理解的) fun Observable.subscribeCrashOnRuntimeException(onNext: (T) -> Unit, onError: (Throwable) -> Unit) { this.subscribe({ onNext(it) }, { e -> if (e is RuntimeException) { throw e } else { onError(e) } }) } fun usageExample() { val observable = Observable.just(1) observable.subscribeCrashOnRuntimeExceptions( { next -> Log.d(“TAG”, “next: $next”) }, { e -> Log.d(“TAG”, “error: $e”) } […]

覆盖意图额外

我使用下面的代码创建通知(Kotlin) val builder = NotificationCompat.Builder(ctx) …….. .setContentIntent(PendingIntent.getActivity(ctx, 891, ctx.newIntent() .putExtra(“id”, member.id) .addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT), 0)) 所以当通知被点击MainActivity将选择用户,从哪个通知到达。 override fun onNewIntent(intent: Intent?) { val id = intent?.getStringExtra(“id”) ?: return selectUser(extra) } 我从2个不同的用户发送2个通知。 点击第一次通知后,它工作正确(id == _ User1UUID)并选择用户。 然后我按回来,从第二个用户发送另一个通知,点击它,意图仍然包含以前的用户ID并选择它(通过断点检查)。 我知道,这是因为FLAG_ACTIVITY_REORDER_TO_FRONT ,但我必须只保留MainActivity一个实例。

ListView:平滑滚动到底部

当一个按钮被点击,我想应用程序顺利滚动到列表视图的底部。 目前这是我的执行(kotlin) listview.post { listview.smoothScrollToPosition(adapter.count – 1) } 然而,这并不总是正常工作:一些列表项目包含一些可扩展的部分(即其可见性可以是可见或无效的视图),最初是隐藏的。 但是,当这些部分展开时,该function有时不会滚动到结尾,而是滚动到结束之前的位置。 我怎样才能可靠地滚动到列表视图的末尾?

用翻新和moshi填充微调

我是Android开发人员(包括java或kotlin)的初学者。 我正在尝试用改造和moshi从json填充微调器,但我不知道如何将其填充到微调器中。 说实话,我不知道Json数据的返回是否正确,因为Log.d()返回不是作为dump()laravel或php的细节。 脚本在活动onCreate(请阅读脚本的评论,我把Log.d()调试结果) Log.d() val task = object : AsyncTask<Void, Void, Response<List>>() { override fun doInBackground(vararg params: Void): Response<List> { val typeAPI = RestAPI() val callResponse = typeAPI.getNews() val response = callResponse.execute() return response } override fun onPostExecute(response: Response<List>) { if (response.isSuccessful) { val news = response.body() Log.d(“test:”, news!![0].data.toString()) // method ‘java.lang.String com.example.mockie.tigaer.api.TypeDataResponse.toString()’ on […]

为什么房间实体不能在Android中使用不可变属性

我一直在探索房间数据库对象映射库,我觉得奇怪。 正如这个答案所暗示的,实体数据模型不能具有不可变属性。 但我检查了谷歌与kotlin持久的例子 , Room工程与不可改变的属性。 请从示例中检查这个数据类。 什么可能是这种行为的原因? 如果我们可以创建不可变的值( val属性),这可能是一个很好的function,因为这限制了程序员在创建对象之后更改唯一标识符(如id)。

如何使用espresso测试DeepLinks

我想为我所有的深层链接添加测试,启动它们并查看是否启动了所需的活动,并断言活动中显示的内容。 我该怎么做? 编辑 我结束了测试只是深入链接匹配,如https://medium.com/@singwai/testing-deep-linking-with-espresso-and-burst-5e1bdb3c5e29说。 在kotlin: @Throws(Exception::class) fun test_deepLink_isResolvedBy(url: String, canonicalActivityName: String) { val appContext = InstrumentationRegistry.getTargetContext() val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)) val resolvedActivities = appContext.packageManager.queryIntentActivities(intent, PackageManager.MATCH_ALL) val resolverActivityMissing = resolvedActivities.none { it.activityInfo.packageName == appContext.packageName && it.activityInfo.name == canonicalActivityName } if (resolverActivityMissing) { fail(“$url is not resolved for $canonicalActivityName”) } } 然后我检查是否有活动解决我的url,并测试每个活动在自己的测试。

NotificationListenerService未创建

也许愚蠢的问题,但我已经花了很多时间在这个。 我有我的Kotlin听众: package pl.bmideas.michal.bmnotifier public class MyNotificationListener : NotificationListenerService() { private var apiService :BackendApi? = null; override fun onCreate() { Log.i(“MyNotificationListener” , “Creating NotificationListenerService service”) super.onCreate() (………SOMETHING ELSE…………..) } override fun onDestroy() { super.onDestroy() Log.i(TAG, “DESTROING”) (………SOMETHING ELSE…………..) } override fun onNotificationRemoved(sbn: StatusBarNotification) { val sbnInfo = StatusBarNotificationExtended(sbn) Log.i(TAG, “REMOVED”) } override fun onNotificationPosted(sbn: StatusBarNotification) […]

将插件升级到3.0.0时出现Gradle构建错误(manifestOutputFile)

将Gradle插件版本升级到3.0.0( classpath “com.android.tools.build:gradle:3.0.0” )后,如果我尝试清理或生成项目,则会出现以下错误: A problem occurred configuring project ‘:app’. > Manifest Tasks does not support the manifestOutputFile property any more, please use the manifestOutputDirectory instead. For more information, please check https://developer.android.com/studio/build/gradle-plugin-3-0-0- migration.html * Try: Run with –stacktrace option to get the stack trace. Run with –info or –debug option to get more log output. […]

如何在Retrofit 2中处理POST请求响应?

我试图将appVersion和deviceName作为String发送到服务器,并获取systemDate和token为字符串作为响应。 这是我在Kotlin的POJO: class InitPost { @SerializedName(“appVersion”) @Expose var versionName: String? = null @SerializedName(“deviceName”) @Expose var manufacturer: String? = null } Api服务: @POST(“getRequest”) Call postInit(@Body InitPost initPost); 在我的活动中: apiService.postInit(initPost) .enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if(response.isSuccessful()){ Timber.d(“INIT POST SUCCESSFUL”); } } @Override public void onFailure(Call call, Throwable t) { Timber.e(“INIT POST […]

Kotlin和Android Databinding

Updated 21.06.2015 我想在同一个项目中尝试kotlin和android数据绑定。 但是,当我添加到kotlin gradle插件依赖我不能建立一个甚至一个空的项目了错误: 无法生成视图绑定器java.lang.NoClassDefFoundError:kotlin / jvm / internal / ExtensionFunctionImpl buildscript { repositories { jcenter() } dependencies { classpath ‘com.android.tools.build:gradle:1.3.0-beta3’ classpath “com.android.databinding:dataBinder:1.0-rc0” classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:0.12.613” } } 任何workaroud使它一起工作?