我正在创建“我的自定义启动器”。在这种情况下,我想将“快速搜索栏”放在我的主屏幕上,即Google Now启动器。
我该怎么办?我已经阅读了多个帖子,但没有找到相关的答案。
我不想显示“小部件选择器”。我希望用户一安装这个启动器就能看到搜索栏。
先感谢您的帮助。
我正在创建“我的自定义启动器”。在这种情况下,我想将“快速搜索栏”放在我的主屏幕上,即Google Now启动器。
我该怎么办?我已经阅读了多个帖子,但没有找到相关的答案。
我不想显示“小部件选择器”。我希望用户一安装这个启动器就能看到搜索栏。
先感谢您的帮助。
https://android.googlesource.com/platform/packages/apps/Launcher3/
如果您检出nougat-mr5分支,可以查看代码并通过从GMS包中获取组件并将其添加为行元素来显示搜索栏。请在代码中寻找QSB。Launcher
类中的getOrCreateQsbBar
方法(我假设您派生了Launcher3)如果无法实例化小部件,则不会请求权限,而是默默地失败。 问题出现在getOrCreateQsbBar
内的此代码片段中: if (!AppWidgetManagerCompat.getInstance(this)
.bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
mAppWidgetHost.deleteAppWidgetId(widgetId);
widgetId = -1;
}
不仅需要将widgetId
重置为-1
,还需要请求安装小部件的权限并再次调用getOrCreateQsbBar
。以下是一个请求权限的示例代码:
boolean hasPermission = appWidgetManager.bindAppWidgetIdIfAllowed(widgetId, searchProvider);
if (!hasPermission)
{
mAppWidgetHost.deleteAppWidgetId(widgetId);
widgetId = -1;
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, searchProvider);
startActivityForResult(intent, REQUEST_BIND);
}
然后在Launcher
类中重载onActivityResult
方法,代码示例如下:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_BIND) {
mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
}
}