androidandroid-serviceandroid-manifestnotification-listener

Service extending NotificationListener never starts


I'm having issues implementing a notification listener. The basic idea is wanting to change the silent mode of the phone, but currently the relevant function(onStartCommand) Is not even being called. The way to silence the phone is to call the enableSilentMode() method which send an intent broadcast internally, built by the getInterruptionFilterRequestIntent() method.

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class SilentModeService extends NotificationListenerService {
    public static int CURRENT_FILTER;

    public static final String ACTION_REQUEST_INTERRUPTION_FILTER =
            SilentModeService.class.getPackage().getName() + '.' + "ACTION_REQUEST_INTERRUPTION_FILTER";
    public static final String EXTRA_FILTER = "filter";

    public SilentModeService() { super();
        CURRENT_FILTER = getCurrentInterruptionFilter();
    }

    public static Intent getInterruptionFilterRequestIntent(Context context, final int filter) {
        Intent request = new Intent(ACTION_REQUEST_INTERRUPTION_FILTER);
        request.setComponent(new ComponentName(context, SilentModeService.class));
        request.setPackage(context.getPackageName());
        request.putExtra(EXTRA_FILTER, filter);
        return request;
    }

    /** Convenience method for sending an {@link android.content.Intent} with {@link #ACTION_REQUEST_INTERRUPTION_FILTER}. */
    private static void requestInterruptionFilter(Context context, final int filter) {
        Intent request = getInterruptionFilterRequestIntent(context, filter);
        context.sendBroadcast(request);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("SilentModeService", "Received start id " + startId + ": " + intent);
        Crashlytics.log(Log.INFO, "SilentModeService", "Requested new filter. StartId: " + startId + ": " + intent);

        return super.onStartCommand(intent, flags, startId);
    }

    public static void enableSilentMode(Context context)
    {
        requestInterruptionFilter(context, INTERRUPTION_FILTER_NONE);
    }

    public static void switchToFilter(Context context, int filter)
    {
        requestInterruptionFilter(context, filter);
    }

    @Override
    public void onCreate() {
        Log.i("SilentModeService", "onCreate");
        super.onCreate();
    }

In my manifest file I have added the lines

    <service android:name=".services.SilentModeService"       
         android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
            <action android:name="com.example.MyEvent.services.ACTION_REQUEST_INTERRUPTION_FILTER" />
        </intent-filter>
    </service>

My OnCreate() method is not called. But I can trigger it and the binding by toggling the security option for notifications.

My way of running the application is using a Genymotion simulated API 21 android.

I have tried fiddling quite a bit, but the onStartCommand() method never seems to be called.

The code is heavily inspired by the Noyze github project: https://github.com/Tombarr/Noyze/blob/master/src/main/AndroidManifest.xml


Solution

  • An easily overlooked mistake...

    In requestInterruptionFilter() use:

    context.startService(request);
    

    instead of

    context.sendBroadcast(request);