2

I am trying to capture the metadatachanged on android for spotify.

I have this receiver code below the activity decleration in my manifest file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.kuzoncode.test">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".MyBroadcastReceiver"
            android:enabled="true"
            android:exported="true">

            <intent-filter>
                <action android:name="com.spotify.music.metadatachanged"/>
            </intent-filter>

        </receiver>
    </application>

</manifest>

MyBroadcastReceiver:

package com.kuzoncode.test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyBroadcastReceiver extends BroadcastReceiver {
    static final class BroadcastTypes {
        static final String SPOTIFY_PACKAGE = "com.spotify.music";
        static final String PLAYBACK_STATE_CHANGED = SPOTIFY_PACKAGE + ".playbackstatechanged";
        static final String QUEUE_CHANGED = SPOTIFY_PACKAGE + ".queuechanged";
        static final String METADATA_CHANGED = SPOTIFY_PACKAGE + ".metadatachanged";
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // This is sent with all broadcasts, regardless of type. The value is taken from
        // System.currentTimeMillis(), which you can compare to in order to determine how
        // old the event is.
        long timeSentInMs = intent.getLongExtra("timeSent", 0L);

        String action = intent.getAction();

        if (action.equals(BroadcastTypes.METADATA_CHANGED)) {
            String trackId = intent.getStringExtra("id");
            String artistName = intent.getStringExtra("artist");
            String albumName = intent.getStringExtra("album");
            String trackName = intent.getStringExtra("track");
            int trackLengthInSec = intent.getIntExtra("length", 0);
            // Do something with extracted information...
        }
    }
}

When I change tracks it doesn't fire. I also added the wifi changed to see if it would pick that up but it didn't. Does anyone know why this isn't firing?

Spotify doumentation is here --> https://developer.spotify.com/documentation/android/guides/android-media-notifications/

Thanks!

Anmol
  • 8,110
  • 9
  • 38
  • 63
Kuzon
  • 782
  • 5
  • 20
  • 49
  • Have you added it to your manifest correctly? Also there is a section in that documentation that states the media notifcations may be disabled and that disabled is the default state. – teh_raab Dec 30 '18 at 11:33
  • show us your manifest – Ashish Kudale Dec 30 '18 at 11:35
  • @teh_raab I added the manifest into the question. Also I did see that and it is enabled. Also I tried with Wifi changed to see if it would fire on that but it didn't. – Kuzon Dec 30 '18 at 11:42
  • I'm on my phone but from what i can see it seems ok. Have you tried adding debug to on receive or stepping through with debugger to see if you're getting anything? Add a breakpoint in onReceive. I just noticed as well.. i don't think the if condition is required as your intent filter is setup just for that! – teh_raab Dec 30 '18 at 11:45
  • @teh_raab I have been putting a break point after the on receive. If I set it up in my main activity it would work. But it doesn't work when put in the manifest. I want it to run even when the app is closed so I need it in the manifest. Is there any way to do that without the manifest? Would prefer to do it that way though – Kuzon Dec 30 '18 at 11:51
  • Check this to add a service. https://stackoverflow.com/q/16824341/9332016 – teh_raab Dec 30 '18 at 12:03
  • @teh_raab But then i would have to start the service everytime the phone reboots. I would really like to be able to just have this sit in the background – Kuzon Dec 30 '18 at 12:31
  • If you are running on Android 8.0+, this will not work by default, as implicit broadcasts are banned. Also, based on those docs, the user has to enable those broadcasts in the Spotify app. – CommonsWare Dec 30 '18 at 12:55
  • @CommonsWare What are the replacements for this 8.0+? – Kuzon Dec 30 '18 at 13:08
  • @Kuzon: On your side, there are no replacements, other than by using a foreground service and registering the receivers dynamically. Spotify could change their broadcats to try to get around this restriction, as I noted in [this blog post](https://commonsware.com/blog/2017/04/11/android-o-implicit-broadcast-ban.html). Google has hinted at banning apps from the Play Store that try those things, though. – CommonsWare Dec 30 '18 at 13:24
  • @CommonsWare I realised that I have pointed this to API level 15 when I created the App. But I still isn't letting me recieve do you know why? – Kuzon Dec 30 '18 at 22:13
  • @Kuzon: "I realised that I have pointed this to API level 15 when I created the App" -- if you mean that your `targetSdkVersion` is 15, that is a problem, as you cannot distribute your app on the Play Store. But, beyond that, perhaps you did not enable the broadcasts in the Spotify app. – CommonsWare Dec 30 '18 at 22:54
  • @CommonsWare, Sorry what do you mean that is my problem? And I did enable the broadcasts because if a put an explict reciever in the code it works. – Kuzon Dec 31 '18 at 08:20
  • @Kuzon: If your `targetSdkVersion` is too low, the Play Store will refuse to distribute your app. 15 is too low. I am guessing that you are referring to the `targetSdkVersion` when you wrote "I realised that I have pointed this to API level 15 when I created the App". – CommonsWare Dec 31 '18 at 12:28

0 Answers0