2

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 .

Community
  • 1
  • 1
android developer
  • 114,585
  • 152
  • 739
  • 1,270

2 Answers2

2

Not sure here, but I think you need to remove the "com" from "com.MyActivity."

<activity android:icon="@drawable/ic_launcher"
  android:label="ShortcutActivity" android:name=".ShortcutCreatorActivity">
   <intent-filter>
    <action android:name="android.intent.action.CREATE_SHORTCUT" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>
LowDev1
  • 1,108
  • 2
  • 10
  • 19
  • no, you are wrong. there are multiple ways to set the "android:name" tag for the activity . the "." in the beginning is just a shortcut to have the prefix from the application package itself. you can do it , and you can specify it exactly as i did. both should work. – android developer Nov 07 '11 at 20:15
1

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

mcveat
  • 1,416
  • 15
  • 34
  • this is weird. i'm pretty sure i've checked it out (btw , you forgot to put ".class" at the second argument) . it worked. thank you. – android developer Nov 07 '11 at 21:13
  • Glad I helped. I fixed it answer too. – mcveat Nov 07 '11 at 21:15
  • i thought it worked, but it only worked for normal project. when i use a library project , it doesn't . in my scenario , i have a library project that has the MyActivity and ShortcutCreatorActivity classes inside it , while the launcher activity resides in the normal project that uses the library project. i do have the manifest to include the correct activities paths, but i keep getting "application not found" . – android developer Nov 08 '11 at 08:15
  • now i get it : i was missing the attribute android:exported="true" . wonder why it is like this , since the library should be a part of the whole app now , so it should be available to it too. can anyone please explain? it's weird since the MyActivity class is already reachable inside the simple project that use the library project (for example , by using "startActivity" . – android developer Nov 08 '11 at 08:29
  • btw, another way to achieve it is by using shortcutIntent.setComponent(new ComponentName(packageName,fullPathToActivity)); – android developer Apr 27 '13 at 15:13
  • Also look at this for more info: http://easybook4u.com/index.php/2017/09/11/how-to-create-a-shortcut-for-non-launcher-activity/ – Himanshu arora Sep 11 '17 at 17:21