How to transfer additional data to the action from the desktop shortcut?

I have a Non-Launcher function with a context menu. The menu has the option of adding activity to the Android main screen as a shortcut.

I use the code below to create a shortcut.

private void ShortcutIcon(){ Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class); shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); Intent addIntent = new Intent(); addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Test"); addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher)); addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); getApplicationContext().sendBroadcast(addIntent); } 

The necessary permissions and intent filters are correctly set. When I run this code, the shortcut is created successfully. When you click on a shortcut, the activity opens as expected.

However, my activity shows some dynamic data. for this I need to pass one small string variable into action.

I tried to use this code before setAction (just like you would pass extra data to a normal intent to trigger an activity)

 addIntent.putExtra("key_primarykey", value_i_want_to_pass); 

However, when the user clicks on the shortcut, inside the action, value_i_want_to_pass appears as Null.

Some applications, such as Whatsapp , allow you to do the same. You can save the chat shortcut. Also, some dialer apps allow you to add a contact as a shortcut so that when you click on the shortcut, a voice call starts automatically.

I want to know how to transfer some data from my shortcut to my activity.

+5
source share
1 answer

You send your data to addIntent , which will be captured by the Launcher broadcast receiver.

Just change the following line

 addIntent.putExtra("key_primarykey", value_i_want_to_pass); 

to

 shortcutIntent.putExtra("key_primarykey", value_i_want_to_pass); 

and write it along with your shorcutIntent code before setting shortcutIntent to addIntent .
So the Action Intent that you set for the shortcut will return you the correct value. Thus, the modified code will look as follows.

 private void ShortcutIcon(){ Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class); shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); shortcutIntent.putExtra("key_primarykey", value_i_want_to_pass); Intent addIntent = new Intent(); addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Test"); addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher)); addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); getApplicationContext().sendBroadcast(addIntent); } 
0
source

Source: https://habr.com/ru/post/1265276/


All Articles