Mam tę prostą usługę, która nadaje bieżącą lokalizację użytkownika. Chcę używać mechanizmu wiążącego tylko do kontrolowania cyklu życia usługi, ale usługa po prostu się nie uruchamia.Usługa nie jest tworzona (lub łączona) po bindService()
Co zrobiłem źle?
public class GPSActivity extends ListActivity {
...
protected void onResume() {
super.onResume();
Log.i("Service", "Service bound");
Intent intent = new Intent(this, LocationService.class);
bindService(intent, service_connection , Context.BIND_AUTO_CREATE);
}
protected void onPause() {
if (dataUpdateReceiver!=null)
unregisterReceiver(dataUpdateReceiver);
unbindService(service_connection);
super.onPause();
}
class LocationServiceConnection implements ServiceConnection{
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("Service", "Service Connected");
}
public void onServiceDisconnected(ComponentName name) {
}
}
}
LocalBinder.java
public class LocalBinder<S> extends Binder {
private String TAG = "LocalBinder";
private WeakReference<S> mService;
public LocalBinder(S service){
mService = new WeakReference<S>(service);
}
public S getService() {
return mService.get();
}
}
LocationService.java
public class LocationService extends Service {
public void onCreate() {
initLocationListener();
Log.i("Location Service","onCreate()");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("Location Service", "Received start id " + startId + ": " + intent);
return START_NOT_STICKY;
}
private final IBinder mBinder = new LocalBinder<LocationService>(this);
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
AndroidManifest.xml
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
...
<service android:name=".LocationService">
</service>
</application>
Edycja: Naprawiono dzięki odpowiedzi NickT.
Manifest wpis nie miał zamiaru filtr lub poprawną nazwę
<service
android:enabled="true"
android:name="com.android.gps.services.LocationService">
<intent-filter>
<action android:name="com.android.gps.services.LocationService" />
</intent-filter>
</service>
a intencje użyłem do wiązania było jak te, które trzeba użyć przy rozpoczynaniu działalności. prawidłowa brzmi:
Intent intent = new Intent("com.android.gps.services.LocationService");
Co twoja 'publicznego IBinder onBind (Intent intent)' 'w sposób LocationService.java' wyglądać? – Jens
@Jens Edytowałem pytanie, aby uwzględnić metodę onBind(). – bughi
Hm. I nawet nie dostajesz 'Log.i (" Usługa lokalizacji "," onCreate() ");' zaloguj się do swojego logcat? – Jens