Shortcuts Vs Launcher Widgets (android)
We have enabled shortcuts for two of our app screen. Using manifest, we have initialized Activity which is referring to the shortcut as below.
Below code I wrote in ShortCut1.java class file. This is the activity code.
publicclassShortCut1extendsActivity{
@OverrideprotectedvoidonCreate(@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. IntentresIntent= getResIntent();
setResult(RESULT_OK, resIntent);
finish();
}
private Intent getResIntent() {
IntentshortcutIntent=newIntent();
// Target intent is set to this own class. So that when the user clicks on the shortcut this intent will be passed.Intenttarget=newIntent(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;
}
privatevoidopenHomeTab(int tabIndex) {
// Final target screen. Intentintent=newIntent(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.
Post a Comment for "Shortcuts Vs Launcher Widgets (android)"