Java – Android: Nested intent service is not started after calling Context.startService().

Android: Nested intent service is not started after calling Context.startService()…. here is a solution to the problem.

Android: Nested intent service is not started after calling Context.startService().

My nested intent service is defined as follows:

package com.my.package;

... // Bunch of imports

public class MyNotifier

... // Bunch of variables

public class MissedCallIntentService extends IntentService {

private static final String TAG = "MissedCallIntentService";

public MissedCallIntentService() {
            super("MissedCallIntentService");
            Log.i(TAG, "Creating intent service.");
        }

@Override
        public void onHandleIntent(Intent intent) {
            Log.i(TAG, "Handling intent service.");
        }
    }

 Test my nested intent filter
    public MyNotifier(Context app) {
        mApp = app;
        Log.i(LOG_TAG, "Going to start intent service.");
        Intent intent = new Intent(mApp, MissedCallIntentService.class);
        mApp.startService(intent);
    }

... // Bunch of functions
}

My AndroidManifest.xml file contains the following:

<?xml version="1.0" encoding="utf-8"?>
<manifest ... >  
     Protected Broadcasts
     Permissions
    <application ... >
        <service android:name="com.my.package.MyNotifier.MissedCallIntentService" >
        </service>

<activity android:name="ActivityOne"    
            android:label="@string/activity_one"
            <intent-filter>
                ...
            </intent-filter>
        </activity>

<activity android:name="ActivityTwo"
            android:label="@string/activity_two"
            <intent-filter>
                ...
            </intent-filter>
        </activity>

<activity android:name="ActivityThree"
            android:label="@string/activity_three"
            <intent-filter>
                ...
            </intent-filter>
        </activity>
    </application>
</manifest>

After building my app and then pushing it to my phone and running it, this is what I saw.

$ make_magic && adb remount && adb push MyApp.apk /system/app/ && adb reboot && adb logcat | grep 'intent\ service'
make: Leaving directory `BuildDir'
remount succeeded
6149 KB/s (6036528 bytes in 0.958s)
- waiting for device -
I/MyNotifier( 1184): Going to start intent service.

I should see:

I/MyNotifier( XXXX): Going to start intent service.
I/MissedCallIntentService( XXXX): Creating intent service.
I/MissedCallIntentService( XXXX): Handling intent service.

That’s where my problem lies. What do I need to add to invoke my intent service?

Solution

Declare the nested inner class as static or define it in its own class (update the list if you do

).

If you are referencing an inner class, the reference should be

<service android:name="com.my.package.MyNotifier$MissedCallIntentService" />

(Note the dollar sign).

Related Problems and Solutions