【问题标题】:How can you create Shortcuts from an Android Widget?如何从 Android Widget 创建快捷方式?
【发布时间】:2020-08-10 19:27:00
【问题描述】:

我正在尝试向我的应用添加新的小部件功能,它允许您从“快捷方式小部件”创建固定快捷方式。

通常在 Android 中创建快捷方式,如下面的视频所示。您长按应用程序,选择一个快捷方式,然后您可以将快捷方式固定到主屏幕上。我的应用目前按照 Android 开发者指南实现了这样的快捷方式。

但是,我注意到设置应用程序允许您为应用程序创建更多快捷方式,方法是允许通过设置小部件创建快捷方式,如下面的视频所示。

我想让我的应用程序的用户能够为我的应用程序中的许多功能创建快捷方式,但是,大多数启动器只能显示四个长按的快捷方式。

有谁知道如何实现一个允许您创建许多快捷方式的应用小部件,就像设置应用已经实现的那样?

【问题讨论】:

    标签: android widget android-widget shortcut android-shortcut


    【解决方案1】:

    虽然它是从 App 小部件部分添加的,但它甚至与 appwidgetprovider 无关。 如果您尝试创建应用小部件,那么您走错了路。

    我从现有项目中获取了一些代码,希望对您有所帮助 :)

    1.向AndroidManifest.xml

    添加所需权限
        <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
    

    2.add intent-filter &lt;action android:name="android.intent.action.CREATE_SHORTCUT"/&gt; ,所以你的 AndroidManifest.xml 应该是这样的:

        <activity
            android:name=".ui.SetupWizardActivity"
            android:label="@string/create_shortcut_label">
            <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT"/>
            </intent-filter>
        </activity> 
    

    添加这些行后,当用户将活动从小部件部分拖到主页时,将打开所需的活动。

    3.创建快捷方式的代码如下:

    import android.content.Context;
    import android.content.Intent;
    import android.widget.Toast;
    
    import androidx.annotation.DrawableRes;
    import androidx.core.content.pm.ShortcutInfoCompat;
    import androidx.core.content.pm.ShortcutManagerCompat;
    import androidx.core.graphics.drawable.IconCompat;
    
    public class ShortcutCreator {
    
        public static void vCreateShortcutByActivityClass(Context context, String strShortcutID, Class<?> ActivityClass, String strLabelName, @DrawableRes int resourceID){
    
            if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)){
                Intent intent = new Intent(context, ActivityClass).setAction(Intent.ACTION_MAIN);
                ShortcutInfoCompat sic =
                        new ShortcutInfoCompat.Builder(context, strShortcutID).setIntent(intent).setShortLabel(strLabelName).setIcon(IconCompat.createWithResource(context, resourceID)).build();
                ShortcutManagerCompat.requestPinShortcut(context, sic, null);
    
            }else{
    
                Toast.makeText(context, "Device doesn't support creating shortcuts.", Toast.LENGTH_SHORT).show();
    
            }
        }
    
    }
    

    注意:ShortcutID 用于判断是否存在重复的快捷方式。

    4.现在您可以对您想要的活动进行以下修改,以调用创建快捷方式的代码(显示的活动 许多用于创建不同快捷方式的选项):

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
        
                final String strIntentAction = getIntent().getAction();
                if (TextUtils.equals(strIntentAction,Intent.ACTION_CREATE_SHORTCUT)) {
                    setContentView(R.layout.activity_shortcut);
                    //display selections here and let user choose shortcut
                    //...deleted
                    vCreateShortcutByActivityClass(this, "ID_0", SetupWizardActivity.class, "App Settings", R.mipmap.ic_launcher);
                    finish();
                    return;
                }
                
                setContentView(R.layout.activity_main);
                //codes of your activity opened from launcher
                //...deleted
            }
    

    或者您也可以创建一个新的空白活动来创建“单个” 快捷方式,无需调用setContentView(),如果您只需要 创建一个快捷方式:

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                vCreateShortcutByActivityClass(this, "ID_0", MainActivity.class, "App Settings", R.mipmap.ic_launcher);
                finish();
            }
    

    注意:对于Android Nougat(7.1)下的设备,该快捷方式尝试打开的目标activity必须至少包含一个intent-filter标签(可以是任何标签,考虑添加&lt;category android:name="android.intent.category.DEFAULT"/&gt; for隐藏的活动),否则快捷方式可能会显示“应用未安装”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-27
      • 2021-11-23
      相关资源
      最近更新 更多