Search code examples
androidandroid-activityshortcutlauncher

how to put a shortcut of an activity into the launcher?


i'm trying to do something that is described briefly on the next link:

link

meaning, i have an activity that can be even on another application (but for now let's focus on an activity that my app has) , which i want to be able to create a shortcut to it.

for terminology , let's say that the activity that creates the shortcut is named "ShortcutCreatorActivity" ,and the activity that gets started is "MyActivity" .

what i got from what is written is that the ShortcutCreatorActivity should be defined in the manifest as:

<activity android:icon="@drawable/ic_launcher"
  android:label="ShortcutActivity" android:name="com.my_app.ShortcutCreatorActivity">
  <intent-filter>
    <action android:name="android.intent.action.CREATE_SHORTCUT" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>

and what i got in its java code is:

public class ShortcutCreatorActivity extends Activity
  {
  @Override
  protected void onCreate(final Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    final Intent shortcutIntent=new Intent("com.my_app.MyActivity");
    final ShortcutIconResource iconResource=Intent.ShortcutIconResource.fromContext(this,R.drawable.ic_launcher);
    final Intent intent=new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,shortcutIntent);
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"Shortcut Test");
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,iconResource);
    setResult(RESULT_OK,intent);
    Toast.makeText(this,"shortcut created",Toast.LENGTH_SHORT).show();
    finish();
    }
  }

yet i keep getting the same message of "application not found" when clicking on the shortcut , and the log :

ActivityManager(232): Starting: Intent { act=com.my_app.MyActivity flg=0x10200000 bnds=[80,150][160,250] } from pid 3956

can anyone please help me? what is missing? i've also tried some intent filters for the MyActivity activity inside the manifest. nothing helped...


@liro i don't think it matters , since i've specified the exact full path to the class , including the package name.


everyone, please, if you have a working project, that would be perfect .


Solution

  • Instead of

    final Intent shortcutIntent=new Intent("com.my_app.MyActivity");
    

    try

    final Intent shortcutIntent=new Intent(this, com.my_app.MyActivity.class);
    

    or define your action in intent-filter of that activity, which I believe you may did not.

    Those two links may be helpful: http://developer.android.com/reference/android/content/Intent.html#Intent(java.lang.String) http://developer.android.com/guide/topics/manifest/action-element.html