【问题标题】:Shortcuts vs Launcher Widgets (Android)快捷方式与启动器小部件 (Android)
【发布时间】:2018-05-25 23:34:20
【问题描述】:

我们为我们的两个应用屏幕启用了快捷方式。使用 manifest,我们已经初始化了 Activity,它指的是下面的快捷方式。

<activity
    android:name=".ui.shortcuts.ShortCut1"
    android:screenOrientation="portrait"
    android:icon="@drawable/shortcut1"
    android:label="@string/app_shortcut_name1"
    android:theme="@style/AppLightTheme">
       <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />
            <action android:name="android.intent.action.CREATE_SHORTCUT" />
       </intent-filter>

 </activity>

从代码中,我启用了如下快捷方式。

Intent shortcutIntent = null;
shortcutIntent = new Intent(ApplicationNekt.getContext(), ShortCut1.class);

shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = new Intent();
intent.putExtra("duplicate", false);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);



intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, ApplicationNekt.getContext().getString(R.string.app_shortcut_name1));
 intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(ApplicationNekt.getContext(), R.drawable.shortcut1));

intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
ApplicationNekt.getContext().sendBroadcast(intent);

现在在 Nova 和 Action 启动器中,它们会在快捷方式部分下显示快捷方式,其中包含我在清单中提供的图标和文本。如果我单击并按住 ,我可以将图标放在主页选项卡上。在那之后,我的目标活动立即打开。但是当我回到手机主屏幕时,他们在上一步中创建的快捷方式图标被删除了。

我错过了什么吗?

【问题讨论】:

    标签: android shortcut launcher android-launcher


    【解决方案1】:

    来自 Nova 启动器的 Kevin 回复了支持电子邮件。

    它也在不同的线程中解释Android define shortcut that can be used in custom launcher

    就我而言,我既有添加代码的快捷方式,也希望支持想要从 Nova/Action Launcher 的小部件屏幕添加快捷方式的用户。所以我做了以下。

    下面是我在 ShortCut1.java 类文件中编写的代码。这是活动代码。

    public class ShortCut1 extends Activity{
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        // This code runs when the user actually clicks and 
        // opens the shortcut. so redirect him to target screen.  
        openTargetTab(0);
    
    
        // This code is useful when called by the Nova/Action launcher's 
        // widget is clicked. So return them with icon, name and target 
        // activity. Once they receive it they will set the short cut icon on home. 
        // Note: Even when the shortcut is clicked, this result is set, 
        //   but nobody reads the response. So it should be ok. 
        Intent resIntent = getResIntent();
        setResult(RESULT_OK, resIntent);
    
        finish();
    }
    
    private Intent getResIntent() {
    
        Intent shortcutIntent = new Intent();
        // Target intent is set to this own class. So that when the user clicks on the shortcut this intent will be passed.
        Intent target = new Intent(this, ShortCut1.class);
        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, target);
        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, 
             Application.getContext().getString(R.string.shortcut_name));
        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(Application.getContext(), 
                                                    R.drawable.shortcut1));
        return shortcutIntent;
    }
    
    private void openHomeTab(int tabIndex) {
    
        // Final target screen. 
        Intent intent = new Intent(this, TargetActivity.class);
        startActivity(intent);
    }
    }
    

    注意:我没有删除或更改清单上的任何代码或添加代码的快捷方式。由于我的应用程序也需要这种支持,因此我保留了该代码。因此,当用户单击“添加快捷方式”时,该代码将运行。我在这里所做的唯一改变是,我以适当的意图调用了“Setresult”,这是第 3 方发射器可以理解的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      • 2019-12-10
      • 2021-09-05
      相关资源
      最近更新 更多