54

I am looking to start an activity in my app using a custom action. I have found a few answers but everything I try it throws java.lang.RuntimeException saying No Activity found to handle Intent { act=com.example.foo.bar.YOUR_ACTION }.

This is the activity in my manifest file:

<activity
    android:name=".FeedbackActivity" >  
    <intent-filter>
        <action android:name="com.example.foo.bar.YOUR_ACTION" />
    </intent-filter>
</activity>

And this is how I'm starting the activity:

Intent intent = new Intent("com.example.foo.bar.YOUR_ACTION");
startActivity(intent);

Any help would be greatly appreciated.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
Jason Crosby
  • 3,533
  • 4
  • 28
  • 49
  • I think you should mark Maks' answer as the correct one (for reference to the other users) – Kzar Aug 24 '14 at 15:10
  • Actually, marking an answer as accepted is supposed to mean that it worked for the OP, not necessarily that it's the best answer for every one else - that's what upvotes are for. – Cullub Jul 29 '15 at 17:05

4 Answers4

143

I think what you need is to add a default category to your intent-filter, eg.

<activity
    android:name=".FeedbackActivity" >  
    <intent-filter>
        <action android:name="com.example.foo.bar.YOUR_ACTION" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

see this answer for more info.

Community
  • 1
  • 1
Maks
  • 7,562
  • 6
  • 43
  • 65
39

I think you are creating your intent wrong. Try like this:

String CUSTOM_ACTION = "com.example.foo.bar.YOUR_ACTION";

//Intent i = new Intent(this, FeedBackActivity.class);  // <--- You might need to do it this way.
Intent i = new Intent();
i.setAction(CUSTOM_ACTION);

startActivity(i);
FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • I'm not getting an exception anymore however it just shows a screen for the user to select an app to complete the action. – Jason Crosby Jun 06 '12 at 22:46
  • 15
    He's doing it [just fine](http://developer.android.com/reference/android/content/Intent.html#Intent(java.lang.String)). He's just missing the DEFAULT category. – TWiStErRob Sep 19 '14 at 20:46
  • 3
    Calling setAction() is the same as calling new Intent(String) constructor – David Dec 08 '15 at 05:17
4

Just add and intent-filter category as Default.

Implicit intent works perfectly and in many cases its better to use a implicit intent with Intent-action to call a service/Activity than using class-name.

Before startActivty() / startService() with proper context you cane use this method 'queryIntentActivities(Intent intent, int flags)' from package manager class.

It helps the ActivityManager (responsible for launching activities) to check whether the Android system is getting any match with you Intent.

If it doesn't it returns a list size 0 or else >0.

By this you can also check if your app is getting the call,and in this case even if your app is not installed / has got some problem, it will not crash but will throw a warning in Log. Users will face no big trouble apart from app not being launched.

(users will never forgive you if tour app crashes).

Hope this will help !!! Happy Coding. :)

Rajesh
  • 10,318
  • 16
  • 44
  • 64
2

I faced the same problem when trying to launch the activity residing in the dynamic feature module and starting through action String as the Activity is not resolvable by name at compile time. So I set the action but the activity crashes every time (No Activity found to handle intent bla bla.. ) until I set the correct package name.

Context c = getApplicationContext();// flag would be require Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag
Intent i = new Intent(action_string);
i.setPackage(context.getPackageName());//this did the trick actually
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);

In the Manifest : add catrgory default to the intent filters from google docs:

<category android:name="android.intent.category.DEFAULT"/>

Note: In order to receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter. The methods startActivity() and startActivityForResult() treat all intents as if they declared the CATEGORY_DEFAULT category. If you do not declare it in your intent filter, no implicit intents will resolve to your activity.

dtrunk
  • 4,685
  • 17
  • 65
  • 109
SaadurRehman
  • 622
  • 8
  • 20