Android服务未从broadcastreceiver

时间:2015-12-29 11:28:30

标签: android service broadcastreceiver spotify

新手在这里。

我试图从广播接收器启动服务,但我无法让服务运行。广播接收器被调用并从spotify应用程序获取数据 - 我的问题是当我尝试启动服务然后没有任何反应。我想启动服务来做网络动作。有更好的方法吗? 广播接收器类,主要来自spotify.com:

package com.bengaard.obnyt;

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

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);
        Log.i("BROADCAST--Spotify", "Spotify");

Intent intentService = new Intent(context, MyService.class);
        // add infos for the service which file to download and where to store
        intentService.putExtra("trackId", trackId);
        intentService.putExtra("artistName", artistName);
        intentService.putExtra("albumName", albumName);
        intentService.putExtra("trackName", trackName);
        intentService.putExtra("trackLengthInSec", trackLengthInSec);
        context.startService(intentService);


        // Do something with extracted information...
    } else if (action.equals(BroadcastTypes.PLAYBACK_STATE_CHANGED)) {
        boolean playing = intent.getBooleanExtra("playing", false);
        int positionInMs = intent.getIntExtra("playbackPosition", 0);

        // Do something with extracted information
    } else if (action.equals(BroadcastTypes.QUEUE_CHANGED)) {
        // Sent only as a notification, your app may want to respond accordingly.
    }
}
}

这是我尝试启动的服务。我的想法是将spotify数据存储在我服务器上的mysql数据库中:

package com.bengaard.obnyt;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.IOException;

public class MyService extends IntentService {

public MyService() {
    super("MyService");
}

public void onCreate() {
    super.onCreate();
    Log.i("sportify Server", ">>>onCreate() spotify");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, startId, startId);
    Log.i("spotify LocalService", "Received start id " + startId + ": " + intent);

    return START_STICKY;
}

@Override
protected void onHandleIntent(Intent intent) {
    Toast.makeText(getApplicationContext(), "Action: " + intent.getAction(), Toast.LENGTH_SHORT).show();
    Log.i("SERVICE-Spotify", "Spotify");
    String trackId = intent.getStringExtra("trackId");
    String artistName = intent.getStringExtra("artistName");
    String albumName = intent.getStringExtra("albumName");
    String trackName = intent.getStringExtra("trackName");
    String trackLengthInSec = intent.getStringExtra("trackLengthInSec");
    HttpClient client = new DefaultHttpClient();
    String url = "http://www.-----.php?trackId=" + trackId + "&artistName=" + artistName + "&albumName=" + albumName + "&trackName=" + trackName + "&trackLengthInSec=" + trackLengthInSec;
    HttpGet request = new HttpGet(url);
    try {
        HttpResponse response = client.execute(request);
    } catch (IOException e) {
        e.printStackTrace();
    }
}}

不幸的是,我可以在日志中看到这个服务永远不会被调用 - 但为什么呢?

我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bengaard.obnyt" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>

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

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


<activity android:name="com.facebook.FacebookActivity"
    android:theme="@android:style/Theme.Translucent.NoTitleBar"
    android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
    android:label="@string/app_name" />

<meta-data
    android:name="com.facebook.sdk.ApplicationId"
    android:value="@string/app_id" />

<provider android:authorities="com.facebook.app.FacebookContentProvider--"
    android:name="com.facebook.FacebookContentProvider"
    android:exported="true"/>

</application>

<service android:enabled="true" android:name="com.bengaard.obnyt.MyService" >
</service>

</manifest>

1 个答案:

答案 0 :(得分:2)

清单中的CollectionView标记必须位于<service>标记内,就像您对活动所做的那样。如果没有在清单中正确声明它,系统就不知道它存在。