是否有可能获得Launcher的快捷方式布局并在我们自己的小部件中使用它?

Per*_*ron 2 android android-widget android-layout android-launcher

我注意到Dropbox快捷方式小部件对指定文件夹的外观与快捷方式布局一致,无论我们使用哪个启动器:

在此输入图像描述 在此输入图像描述

所以我想制作一个行为和看起来像快捷方式的小部件,并与用户的启动器保持一致.

是否有可能获得Launcher的快捷方式布局并在我们自己的小部件中使用它?

Kev*_*oil 6

除此之外AppWidgets,Android还有一个Launcher快捷方式的概念,通常分组在"Widget"标签下.Dropbox文件夹是Launcher快捷方式.

快捷方式很简单,所有数据(图标,标签,意图)都是静态的,在创建时确定.它们无法动态更新,调整大小或滚动.但它们与发射器样式匹配(并且可以放置在文件夹或扩展坞内)并且使用的资源少于AppWidgets.

可悲的是,他们的记录很差.你可以在ApiDemos/src/com/example/android/apis/app/LauncherShortcuts.java中找到一个样本(https://android.googlesource.com/platform/development/+/master/samples/ApiDemos/src/com /example/android/apis/app/LauncherShortcuts.java)并在https://developer.android.com/reference/android/content/Intent.html#EXTRA_SHORTCUT_ICON(所有EXTRA_SHORTCUT _...项目)中引用它们.

您需要一个Activityand AndroidManifestintent-filter来处理创建快捷方式:

AndroidManifest.xml

<activity
    android:name=".LauncherShortcutActivity" >
    <intent-filter>
        <action android:name="android.intent.action.CREATE_SHORTCUT" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)

此活动将由启动器调用startActivityForResult,您可以提供一个界面(例如,让用户选择快捷方式应该指向的文件夹),但最终必须向启动器返回图标,标签和意图.

void setResult(CharSequence title, int iconResourceId, Intent targetIntent) {
    Intent data = new Intent();
    data.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, iconResourceId));
    data.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
    data.putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent);
    setResult(Activity.RESULT_OK, data);
    finish();
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,图标被指定为我的应用程序的资源.或者,图标可以是位图(例如联系人照片):

    data.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);
Run Code Online (Sandbox Code Playgroud)