2015-12-29 52 views
0

新手在这里。从广播接收器开始的Android服务

我试图从broadcastreceiver启动服务,但我无法让服务运行。 broadcastreceiver被调用并从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. 
    } 
} 
} 

这里是我试图启动该服务。我们的想法是在MySQL数据库Spotify的数据存储在我的服务器上:

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> 

回答

2

清单中的<service>标签需要是<application>标签里面,就像你与活动一样。如果没有在清单中正确声明,系统不知道它存在。

+0

哦基督.. :-)谢谢 – bengaard