Android 服务未从 broadcastreceiver 启动

Android service not started from broadcastreceiver

这里是新手。

我正在尝试从广播接收器启动服务,但我无法获得该服务 运行。 broadcastreceiver 被调用并从 spotify 应用程序获取数据 - 我的问题是当我尝试启动服务时没有任何反应。我想启动服务来执行网络操作。有更好的方法吗? 广播接收器 class,主要是从 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>

清单中的 <service> 标签需要位于 <application> 标签内,就像您对 activity 所做的一样。如果没有在清单中正确声明,系统将不知道它的存在。