0

I am trying to create an app that gives me the GPS coordinates when a photo is taken. Because the GPS needs to be accurate I am running it as a service to get the best lock. However when the main activity is opened the startservice() in onCreate() does not access the onStartCommand in GPSClass.

It runs the onStart() and then is for some reason triggered to access that class after finishing both starting procedures.

Why is this happening? It seems so illogical. By the way, I have been trying to walk through using debugging.

Main activity start up code

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent GPSstart = new Intent(this, GPSClass.class);
    this.startService(GPSstart);

}

private void GPSrunner() {
    Intent GPSstart = new Intent(this, GPSClass.class);
    this.startService(GPSstart);

    // check GPS is onstart
    if (!GPSClass.isGPSon()) {
        buildAlertMessageNoGps();
    }
}

  @Override
  protected void onStart() {
    super.onStart();
    GPSrunner();

  }

Service Code

public class GPSClass extends Service implements LocationListener {

private static final int TWO_MINUTES = 1000 * 60 * 2;
private static LocationManager locationManager;
private static boolean GPSOn;
private static Location location;

@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    GPSOn = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    Toast.makeText(getApplicationContext(),"Service started",Toast.LENGTH_SHORT).show();
    return START_STICKY;

}

@ Override
public void onDestroy() {
    locationManager.removeUpdates(this);

public static boolean isGPSon() {
    return GPSOn;
}

Manifest

<service android:name=".GPSClass" 
        android:exported="false"
        />

    <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>
Luís Cruz
  • 14,780
  • 16
  • 68
  • 100
emmistar
  • 101
  • 10

2 Answers2

0

Shouldn't you declare your service in the manifest?

<application

  .
  .
  .

    <service
        android:name=".MyService"
        android:label="My Service" >
    </service>
</application>
Renato Parreira
  • 838
  • 1
  • 7
  • 23
  • sorry, i did just it didn't print that section when I posted the question, And I can get the service to work, it just is working after all the calls in onCreate and onStart have happened – emmistar Nov 20 '14 at 23:24
  • @emmistar why are you using in both onCreate and onStart? – Renato Parreira Nov 21 '14 at 01:27
  • I am using the onStart() for if the activity is restarted so that it opens GPS again. I may actually change that later down the track. – emmistar Nov 21 '14 at 02:03
  • @emmistar try to use onCreate method instead of onStartCommand in the service, like in this example http://stackoverflow.com/questions/8828639/android-get-gps-location-via-a-service – Renato Parreira Nov 21 '14 at 02:45
  • @emmistar or take a look at this example: http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/ maybe can help you out – Renato Parreira Nov 21 '14 at 02:47
0

I have been doing a lot of research and this is the best answer that I have.

Android activities start services after they have completed onCreate and onStart commands, therefore I found an alternative way to gather the information that I needed for my project.

This conclusion was backed up by another post that I found Android Service never appears to start - onStartCommand() not called

Community
  • 1
  • 1
emmistar
  • 101
  • 10