Search code examples
javaandroiddevice-admin

Android Device Administration AndroidManifest.xml, why the question mark in the receiver name?


In reading the Android documentation on Device Administration, it says to set it up as follows:

<activity android:name=".app.DeviceAdminSample" ... ></activity>

<receiver android:name=".app.DeviceAdminSample$DeviceAdminSampleReceiver" ... ></receiver>

public class DeviceAdminSample extends DeviceAdminReceiver { ... }

When setting it up on my application, on the receiver name, Studio gave a "Cannot resolve symbol" error on the ...$DeviceAdminSampleReceiver" part, so I set my receiver name and class name as "MyDeviceAdminReceiver"

The receiver name in the example doesn't match the Device Administration Receiver class name. What's the significance of the dollar sign? Why does the example have a different name for the receiver and the receiver class?


Solution

  • The "$" syntax indicates that DeviceAdminSampleReceiver is an inner class of DeviceAdminSample. This is Java notation.

    The documentation link you posted, refers to a sample that is distributed with the Android SDK:

    SDK_ROOT/ApiDemos/app/src/main/java/com/example/android/apis/app/DeviceAdminSample.java

    In this sample, you can see that the above class relationship is the case:

    public class DeviceAdminSample extends PreferenceActivity {
       ...
       public static class DeviceAdminSampleReceiver extends DeviceAdminReceiver {
       ...
       }
    }
    

    Thus if you want to follow the same pattern as the example, then you must have the same inner-class relationship. Although nothing in Android requires you to do this. You simply need to make sure that the receiver element in your manifests points at an existing class (be it inner or not).