快捷方式与启动器小部件(Android)
我们已经为两个应用程序屏幕启用了快捷方式。使用manifest,我们初始化了引用快捷方式的活动,如下所示快捷方式与启动器小部件(Android),android,shortcut,launcher,android-launcher,Android,Shortcut,Launcher,Android Launcher,我们已经为两个应用程序屏幕启用了快捷方式。使用manifest,我们初始化了引用快捷方式的活动,如下所示 <activity android:name=".ui.shortcuts.ShortCut1" android:screenOrientation="portrait" android:icon="@drawable/shortcut1" android:label="@string/app_shortcut_name1" android:th
<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>
现在在Nova和Action launchers中,它们在快捷方式部分下显示快捷方式,并带有我在清单中给出的图标和文本。如果单击并按住,我可以将图标放置在“主页”选项卡上。之后,我的目标活动立即打开。但当我返回到手机主屏幕时,他们在上一步中创建的快捷方式图标被删除
我在这里遗漏了什么吗?来自Nova launcher的Kevin回复了支持电子邮件 它也在不同的线程中解释 在我的例子中,我既有快捷方式添加代码,也希望支持希望从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”,并具有适当的意图,这是第三方启动器可以理解的
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);
}
}