CQM CQM - 2 months ago 5x
Android Question

Check if an intent is an Activity or Service or none

I have an ArrayList of Intents, how can I check that the intent at a given position is containing an Activity, Service or nothing?

Intent intent = intents.get(id);

How would you check that the intent at index id could even be properly passed through
, which has an emphasis on Activity classes.


Well it's not a elegant solution but you can try to get class name of intent like this:

Intent it = new Intent(ctx, AnotherActivity.class);
it.getComponent().getClassName() // output com.package.app.MainActivity

Intent it = new Intent(ctx, JustService.class);
it.getComponent().getClassName() // output com.package.app.JustService

and use

switch (...getClassName()) {
    case 'com.package.app.JustService' {

You can use getShortClassName() method and it will output class name without default package:


For empty intent i would check if is null.