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>
哦基督.. :-)謝謝 – bengaard