我的用例如下:在活动A中,我有一个带有可折叠SearchView的操作栏.当用户提出查询并按下"搜索"按钮时,我想显示活动B的结果.我没有这样做,这是我的代码:
searchable.xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/action_search_hint"
android:label="@string/app_name"/>
Run Code Online (Sandbox Code Playgroud)
活动A:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
Run Code Online (Sandbox Code Playgroud)
活动A和B的AndroidManifest:
<activity
android:name=".ActivityA">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".ActivityB">
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
Run Code Online (Sandbox Code Playgroud)
Howerver,无论我尝试什么,A中的getSearchableInfo始终返回null.
我感觉我不理解某些东西.我可以使getSearchableInfo()返回非null的唯一方法是将SEARCH意图过滤器和可搜索的元数据放入清单中的活动A. 但是,当我按下"搜索"按钮时,活动A的另一个实例再次在当前的实例之上启动,这绝对不是我想要的.我想我可以将singleTop添加到A并在onNewIntent中处理SEARCH动作并启动B,但我通常不希望A成为singleTop,我想要默认值.或者我呢?
我尝试在清单中将其添加到A中(如其他一些线程所示):
<meta-data
android:name="android.app.default_searchable"
android:value=".ActivityB" >
</meta-data>
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
编辑:我用A替换了A中的'getComponentName()'行:
searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, ActivityB.class)));
Run Code Online (Sandbox Code Playgroud)
它似乎按我想要的方式工作!我在这里做错了什么,或者这是正确的方法吗?我有点不确定,因为它在任何地方都没有提到!
以下代码:
Observable
.just(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
.doOnNext(item -> System.out.println("source emitting " + item))
.groupBy(item -> {
System.out.println("groupBy called for " + item);
return item % 3;
})
.subscribe(observable -> {
System.out.println("got observable " + observable + " for key " + observable.getKey());
observable.subscribe(item -> {
System.out.println("key " + observable.getKey() + ", item " + item);
});
});
Run Code Online (Sandbox Code Playgroud)
让我感到困惑.我得到的输出是:
source emitting 0
groupBy called for 0
got observable rx.observables.GroupedObservable@42110406 for key 0
key 0, item …Run Code Online (Sandbox Code Playgroud) 我使用Python3.5.1和Requests2.9.1.我的用例如下:我需要从服务T验证(获取令牌)并Authorization在向资源服务器R发出请求时将其用作标头的值.令牌在某个时刻到期并且需要获取新的令牌.
我有一个使用的应用程序requests,在启动时首先获取一个令牌并记住它 - 设置Session用于对R的请求.从那时起3分钟,一切都作为一个魅力.3分钟后,我得到unauthorized回复,因为令牌无效.
我Session用于所有请求,除了要进行身份验证的请求; 此调用更新了要使用的其他请求的Authorization标头Session.
我创建了代码,以便在unauthorized检测到时自动重新进行身份验证,使用response钩子(仅在上面设置Session),这里是代码:
def __hook(self, res, *args, **kwargs):
if res.status_code == requests.codes.unauthorized:
print('Token expired, refreshing')
self.auth() # sets the token on self.__session
req = res.request
print('Resending request', req.method, req.url, req.headers)
req.headers['Authorization'] = self.__session.headers['Authorization'] # why is it needed?
return self.__session.send(res.request)
Run Code Online (Sandbox Code Playgroud)
基本上,它甚至可以工作.但是有几个问题:
为什么有必要Authorization在请求上重新设置标头,即使会话已更新并用于重新发送原始请求?如果没有该行,应用程序将继续刷新令牌,因为新的令牌从未使用过,这可以在输出中看到,令牌是导致自动刷新的原始令牌.
如何使代码更加健壮,即防止无休止的递归(我不确定它是否可能在现实中,但是Authorization在重试的请求中设置标题的行将继续进行)?我正在考虑设置一个自定义标头,如果挂钩发现失败的请求有它,它将不会重新进行身份验证和重新发送.有更好的吗?
编辑:事实证明,如果配置错误,可能会得到(几乎)无限循环(毕竟它是递归的):令牌用于一个环境(如STAGING),但资源服务器来自另一个( TEST) …
我有一个相对较小且简单的应用程序,它始终生成约 17 MB 的发布 APK 文件。不久前,我注意到 APK 大小增加到了惊人的(对于这个应用程序)39 MB。我追踪了导致它的更改,结果发现相同的代码库,其中唯一的更改是minSdkVersion从 16 到 26 ,没有任何其他更改导致 APK 增加。
奇怪的是,当我解压 APK 时,解压后的目录在磁盘上占用了约 40 MB 的空间。有一些更改,但所有更改都在非常小的文件中,就像 26 版本中缺少一些布局一样,可以忽略不计。40 MB 中最大的部分是 lib 文件夹,其中包含 *.so 库,总计 37 MB,但它们在两个 APK 版本中是相同的。(该应用程序是使用 构建的 Flutter 应用程序flutter build apk --release。)
我实际上不希望也不需要minSdkVersion26,并且会恢复此更改,但我很好奇:
minSdkVersion版本之间的尺寸急剧增加?minSdkVersion16 版本时的压缩效果更好?minSdkVersion在他们的设备上占用更多空间吗?我有一个抽象类,它有 2 个受保护的 final 字段,它们在构造函数中设置。该类不使用字段,但所有子类都将使用 - 因此字段在超类中设置并受到保护。
FindBugs 抱怨以下警告:
URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD:未读公共/受保护字段
永远不会读取此字段。该字段是公共的或受保护的,因此它可能旨在与不被视为分析一部分的类一起使用。如果没有,请考虑将其从课程中删除。
我知道我可以抑制警告,但总的来说 - 这样的代码是坏的吗?为什么它首先抱怨?它确实知道它是一个抽象类,即用于子类化?
我正在尝试通过创建 Firestore 文档(消息)时触发的 Firebase 云函数中的 FCM 向主题发送消息。订阅主题(也使用函数完成)并触发发送函数工作正常,但实际发送失败,并显示:
Error: An error occurred when trying to authenticate to the FCM servers. Make sure the credential used to authenticate this SDK has the proper permissions. See https://firebase.google.com/docs/admin/setup for setup instructions.
Run Code Online (Sandbox Code Playgroud)
以及一些包含<H1>PROJECT_NOT_PERMITTED</H1>和的原始 HTML <H1>PROJECT_NOT_PERMITTED</H1> 。
这是我的代码(index.ts):
Error: An error occurred when trying to authenticate to the FCM servers. Make sure the credential used to authenticate this SDK has the proper permissions. See https://firebase.google.com/docs/admin/setup for setup instructions.
Run Code Online (Sandbox Code Playgroud)
和(messages.ts):
import * as admin from …Run Code Online (Sandbox Code Playgroud) firebase typescript google-cloud-functions firebase-cloud-messaging firebase-admin
最小和目标 SDK 为 21 (Lollipop),不使用支持库。
我正在尝试创建一个浮动操作按钮。到目前为止一切顺利,它确实有效(基于其他一些 SO 线程),代码如下:
attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="fabStyle" format="reference"/>
</resources>
Run Code Online (Sandbox Code Playgroud)样式.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme.Material.Light.NoActionBar">
<item name="floatingActionButtonStyle">@style/FabStyle</item>
</style>
<style name="FabStyle" parent="android:Widget.ImageButton">
<item name="android:background">@drawable/ripple</item>
<item name="android:elevation">4dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)(我希望按钮继承默认的 ImageButton 特性,并且看起来工作正常,这是正确的方法吗?)
视图代码(注意第二个构造函数中自定义样式属性的用法):
public class FloatingActionImageButton extends ImageButton {
public FloatingActionImageButton(Context context) {
this(context, null);
}
public FloatingActionImageButton(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.floatingActionButtonStyle);
}
public FloatingActionImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public FloatingActionImageButton(Context …Run Code Online (Sandbox Code Playgroud)我试图在我的应用程序中使用SAF选择器来查找我的自定义文件类型.当文件具有mime类型时,通过调用setType("text/plain")或类似方法,这样做很容易.
如果我想查找所有带有'.blah'扩展名的文件,那么这个电话会是什么样的?
编辑:更多信息:文件类型实际上是具有更改的扩展名的纯文本文件.在文件中,存在一些文本,我解析它从中创建一些数据结构.但是,当用户查找特定扩展名的文件时,我希望他们只查看我的自定义文件,而不是所有文本文件.
我正在使用Lollipop上的新工具栏(没有支持库).我的活动有一个列表,如果列表为空,我不想显示任何选项菜单.我的实现看起来像这样:
在onCreate()中:
setActionBar((Toolbar) findViewById(R.id.toolbar));
getActionBar().setDisplayHomeAsUpEnabled(true);
Run Code Online (Sandbox Code Playgroud)
菜单方法:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (tasks.isEmpty()) {
return false;
}
getMenuInflater().inflate(R.menu.menu_tasks, menu);
return true;
}
Run Code Online (Sandbox Code Playgroud)
当方法膨胀菜单并返回true时,'up'导航箭头工作文件; 当方法返回false时,箭头就在那里,但点击它不会做任何事情.这是某种Android漏洞吗?
我有一个ListView它的排序取决于它的项目状态:Item模型对象有一个bool属性,isActive,并且所有Item值为 的 strue在所有值为 的 s之前排序false。点击一个项目会切换它的isActive状态。设置状态的逻辑不是在列表本身中完成的;相反,它由 BLoC 执行,它也知道如何对项目进行排序。结果,所有列表得到的是一个List<Item>正确排序的流。
目前,结果非常不和谐 - 被点击的项目从它的位置消失并出现在列表中的其他地方,我想让它更平滑,比如可能动画位置变化 - 就像什么ReorderableListView一样,但以编程方式。我不知道该怎么做,我开始胡思乱想,AnimatedList但我不确定这是否适合我的用例 - 它可以为删除和添加设置动画,这不是我真正在做什么(我可以添加一个立即删除,然后添加,但这似乎很复杂)。
这是我到目前为止的代码(简化),它支持动画移除:
class ItemListWidget extends StatelessWidget {
final items = [
'Item A',
'Item B',
'Item C',
'Item D',
'Item E',
];
@override
Widget build(BuildContext context) {
return AnimatedList(
initialItemCount: items.length,
itemBuilder: (context, index, animation) {
final item = items[index];
return ListTile(
title: Text(item),
onTap: …Run Code Online (Sandbox Code Playgroud) android ×5
android-menu ×1
apk ×1
findbugs ×1
firebase ×1
flutter ×1
java ×1
python ×1
rx-java ×1
typescript ×1