- Forum posts: 1
Sep 17, 2014, 6:37:53 PM via Website
Sep 17, 2014 6:37:53 PM via Website
I have some problems with using a Background Location Service for android. I started using this code and modified it for my needs: "gist.github.com/blackcj/20efe2ac885c7297a676"
Here are the modified parts for LocationLoggerServiceManager:
I changed the part, so I can start this service manually with an own broadcast.
public class LocationLoggerServiceManager extends BroadcastReceiver {
private SharedPreferences mPrefs;
public static final String TAG = "LocationLoggerServiceManager";
@Override
public void onReceive(Context context, Intent intent) {
// Make sure we are getting the right intent
if( "android.intent.action.BOOT_COMPLETED".equals(intent.getAction()) || "ftm.vem_game.services.LocationLoggerServiceManager".equals((intent.getAction()))) {
boolean mUpdatesRequested = false;
// Open the shared preferences
mPrefs = context.getSharedPreferences("ftm.vem_game.shared_preferences",
Context.MODE_PRIVATE);
/*
* Get any previous setting for location updates
* Gets "false" if an error occurs
*/
if (mPrefs.contains("KEY_UPDATES_ON")) {
mUpdatesRequested = mPrefs.getBoolean("KEY_UPDATES_ON", false);
}
if(mUpdatesRequested){
//ComponentName comp = new ComponentName(context.getPackageName(), BackgroundLocationService.class.getName());
//ComponentName service = context.startService(new Intent().setComponent(comp));
Intent i = new Intent(context, BackgroundLocationService.class);
ComponentName service = context.startService(i);
if (null == service){
// something really wrong here
Log.e(TAG, "Could not start service BackgroundLocationService");
}
}
} else {
Log.e(TAG, "Received unexpected intent " + intent.toString());
}
}
}
And here is the part where I send the Broadcast in my MainActivity class:
public static final String BROADCAST = "ftm.vem_game.services.LocationLoggerServiceManager";
SharedPreferences sharedPref = getSharedPreferences("ftm.vem_game.shared_preferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("KEY_UPDATES_ON", true);
editor.commit();
Intent intent = new Intent(BROADCAST);
Bundle extras = new Bundle();
extras.putString("send_data", "test");
intent.putExtras(extras);
sendBroadcast(intent);
On the service class I changed nothing. The problem is the service never starts, in the Log it says "Could not start service BackgroundLocationService". And context.startService() returns everytime null.
I dont know what I do wrong or maybe I miss something to do before starting the service.
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.