问题
We have enabled shortcuts for two of our app screen. Using manifest, we have initialized Activity which is referring to the shortcut as below.
<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>
From code I have enabled the shortcuts as follows.
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);
Now In Nova and Action launchers, they display the shortcuts under shortcuts section with the icon and text i gave in manifest. If I click and hold , I am able to place the icon on home tab. Immediately after that, my target activity opens. But the when I go back to phone home screen, shortcut icon they created in previous step was removed.
Am I missing something here?
回答1:
Kevin from Nova launcher replied to support email.
Its also explained in different thread Android define shortcut that can be used in custom launcher
In my case, I have both shortcut adding code as well as I want to support users who wants to add shortcuts from Nova/Action Launcher's Widget screen. So I did the following.
Below code I wrote in ShortCut1.java class file. This is the activity code.
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);
}
}
NOTE: I have not removed or changed any code on Manifest or the shortcut adding code. As I need that support also in my app, I left that code as it is. So when the user clicks on "Add shortcut", that code will run. Only change I did here is, I have called "Setresult" with proper intent which is understandable by 3rd party launchers.
来源:https://stackoverflow.com/questions/47769971/shortcuts-vs-launcher-widgets-android