Android: Nested Intent Service Not Being Started After Call to Context.startService()

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-04 01:56:29

问题


My nested intent services 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 has the following in it:

<?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 then pushing it to the phone and running it this is all I see.

$ 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.

Thus the point of my question. What do I need to add to get my intent service called?


回答1:


Declare the nested inner class as static or define it in it's own class (and update the manifest if you do)

And if you reference an inner class the reference should be

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

(note the dollar sign)



来源:https://stackoverflow.com/questions/19126215/android-nested-intent-service-not-being-started-after-call-to-context-startserv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!