CQM CQM - 3 months ago 18
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);
context.startActivityForResult(intent,1);


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

Answer

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' {
         //todo
    }
}

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

.JustService

For empty intent i would check if is null.

Comments