根据Android版本启动不同的服务

时间:2014-11-27 21:29:30

标签: android service notifications version android-4.4-kitkat

我的Android应用程序应该控制其他音乐播放器(如Poweramp,Google Music ......)。 到目前为止,我在NotificationListenerService中使用了RemoteController类。 该服务在Manifest文件中声明。

但现在在Lollipop中,RemoteController类已弃用,我想使用MediaController(也在NotificationListenerService中 - 也在Manifest中声明)

<service
        android:name=".......MusicReceiverKitKat"
        android:label="@string/service_KitKat"
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
        android:enabled="true" android:exported="true">>
        <intent-filter>

            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
</service>


<service
        android:name="......MusicReceiverLollipop"
        android:label="@string/service_Lollipop"
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
        android:enabled="true" android:exported="true">
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
</service>

但现在,这两项服务都是在KitKat和Lollipop设备上启动的。此外,通知访问列表中还会显示两个应用程序。

但我只想在Android 5中启动Lollipop服务,在Android 4.4中启动KitKat服务。我还想只在通知访问列表中显示特定的应用程序。

我的解决方案是检查服务的onCreate()方法,如果它是正确的Android版本,如果没有,则返回。但我认为必须有更好的方法。

如果有人可以帮助我,那将是善意的。谢谢!

更新

预先确定我的问题: 问题是,如果我在清单文件中声明服务,它们会自动启动 - 独立于Android版本。对于棒棒糖,这是有效的,但是对于KitKat它会崩溃,因为我在Lollipop-Class中使用了@TargetApi(Build.VERSION_CODES.LOLLIPOP),因此无法找到这个类。我想把两个类放在一个并决定使用onCreate()方法,使用哪个对象(RemoteController或MediaController),但我觉得这很难看。

另外,在应用列表中只有一个项目可以拥有通知访问权限。但我认为,如果我在清单文件中声明服务,它们将自动出现在此列表中。

2 个答案:

答案 0 :(得分:2)

我刚想通了,怎么做。 关键是“启用”标志,您可以在清单文件中提供服务。

然后你就可以这样做https://stackoverflow.com/a/23845884/1894572

为v-21(Lollipop)创建了一个“普通”bool.xml和一个bool.xml。

这是正常的:

<resources>
    <bool name="remoteControllerService_enabled">true</bool>
    <bool name="mediaControllerService_enabled">false</bool>
</resources>

这是v-21的那个:

<resources>
    <bool name="remoteControllerService_enabled">false</bool>
    <bool name="mediaControllerService_enabled">true</bool>
</resources>

然后在清单文件中:

<service
        android:name="....MusicReceiverKitKat"
        android:label="@string/service_KitKat"
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
        android:enabled="@bool/remoteControllerService_enabled" android:exported="true">
        <intent-filter>
            <action   android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
    </service>

<service
        android:name="....MusicReceiverLollipop"
        android:label="@string/service_Lollipop"
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
        android:enabled="@bool/mediaControllerService_enabled" android:exported="true">
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
    </service>

这可以按预期工作。

答案 1 :(得分:0)

首先,您必须阅读Supporting Different Platform Version。您可以检查Android构建版本并将其放在if语句中。例如:

if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP){ // for lollipop
    // your RemoteController class for lollipop is loaded here

} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT){ // for kitkat
    // your RemoteController class for kitkat is loaded here

} else { // if above statements is not fulfilled
    // you can use your default RemoteController class here
}

将此代码放入NotificationListenerService。最重要的是您必须了解使用else-if语句。

相关问题