1

In some of our Activities, we've overridden getSystemService() method to return some custom objects for custom service names. It works well for us, to pass down some object to a View somewhere deep inside such Activity.

But in Android Studio we get an error generated by a lint inspection in a line that gets our custom system service:

return (CustomService) context.getSystemService(SERVICE_NAME);

I know lint uses a @ServiceName annotation on getSystemService() which is defined via a @StringDef in Context class. I would like to know how I can extend this definition to include our custom service names.

I wouldn't like to turn of this very usefull inspection nor suppress this error every time we use our custom system service. That's why I hope it is possible to add custom names for lint to recognize that this call is correct.

Marcin Koziński
  • 10,835
  • 3
  • 47
  • 61
  • 1
    If editing Context.java is not an option (i.e., you're not building a custom ROM) then there's nothing you can do apart from maybe suppressing the error somehow -- [this](http://stackoverflow.com/q/27474142/507761) might do it? – Matthew Read Jul 09 '15 at 16:03

1 Answers1

0
public class SomeClass{

    public static final String SERVICE_1= "SERVICE_1";
    public static final String SERVICE_2= "SERVICE_2";
    public static final String SERVICE_3= "SERVICE_1";

    @StringDef({SERVICE_1, SERVICE_2, SERVICE_3})
    @Retention(RetentionPolicy.SOURCE)
    public @interface ServiceName{}

    ...

    public Object getService(@ServiceName String name){
        //Resolve service by string name
        ...
    }

}

to use @StringDef annotation add following dependencies to gradle build file

dependencies {
    compile 'com.android.support:support-annotations:23.1.1'
}

For more info use Support Annotations

Nikolay Nikiforchuk
  • 1,998
  • 24
  • 20