服务扩展NotificationListener永远不会启动

时间:2016-02-02 21:25:04

标签: android android-service android-manifest notification-listener

我在实现通知侦听器时遇到问题。基本的想法是想要改变手机的静音模式,但目前相关功能(onStartCommand)甚至没有被调用。使手机静音的方法是调用enableSilentMode()方法,该方法在内部发送意图广播,由getInterruptionFilterRequestIntent()方法构建。

@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();
    }

在我的清单文件中,我添加了行

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

我的OnCreate()方法未被调用。但我可以通过切换通知的安全选项来触发它和绑定。

我运行应用程序的方法是使用Genymotion模拟API 21 android。

我试过很多东西,但似乎似乎没有调用onStartCommand()方法。

该代码深受Noyze github项目的启发: https://github.com/Tombarr/Noyze/blob/master/src/main/AndroidManifest.xml

1 个答案:

答案 0 :(得分:2)

一个容易被忽视的错误......

requestInterruptionFilter()中使用:

context.startService(request);

而不是

context.sendBroadcast(request);