androidgpsandroid-2.3-gingerbreadandroid-4.4-kitkatandroid-4.2-jelly-bean

GPS program works on JellyBean and KitKat but not Gingerbread


I am have written an app to find the GPS coordinates. The program works totally fine on Android 4.3 and 4.4.2 but for some reason its not working on 2.3.4 and 2.3.6. The GPS is not even turning on. Is there something additional that needs to be done to make it compatible with older APIs? I have included the following permissions in the manifest:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_GPS" /> 

This is the code :

package com.hari.gps;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
public static Context mContext;

public static Context getContext() {
    return mContext;
}

public void setContext(Context mContext) {
    MainActivity.mContext = mContext;
}
private LocationManager locationManager;
private String provider;
public static float lat, lng;
public static TextView t3, t4, t5, t6;
// SMSReceiver s;
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    latituteField = (TextView) findViewById(R.id.text1);
    longitudeField = (TextView) findViewById(R.id.text2);
    // Get the location manager
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // Define the criteria how to select the locatioin provider -> use
    // default
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(provider);

    // Initialize the location fields
    if (location != null) {
        System.out.println("Provider " + provider + " has been selected.");
        onLocationChanged(location);
    } else {
        latituteField.setText("Location not available");
        longitudeField.setText("Location not available");
    }

}

/* Request updates at startup */
@Override
protected void onResume() {
    super.onResume();
    // s.onReceive(getApplicationContext(), getIntent());
    //
    // t3.setText(s.messageReceived);
    locationManager.requestLocationUpdates(provider, 400, 1, this);
}

public void msg(View view) {
    EditText e1 = (EditText) findViewById(R.id.edit);
    String phoneno = "8056371433";
    String s = e1.getText().toString();
    String message, m1, m2;
    t3 = (TextView) findViewById(R.id.text3);
    t4 = (TextView) findViewById(R.id.text4);
    t5 = (TextView) findViewById(R.id.text5);
    t6 = (TextView) findViewById(R.id.text6);
    m1 = String.valueOf(lat);
    m2 = String.valueOf(lng);
    message = m1 + " " + m2;
    if (e1.getText().length() == 0)
        sendSMS(phoneno, message);
    else
        sendSMS(s, message);
}

private void sendSMS(String phoneNumber, String message) {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    PendingIntent sentPI = PendingIntent.getBroadcast(MainActivity.this, 0,
            new Intent(SENT), 0);

    PendingIntent deliveredPI = PendingIntent.getBroadcast(
            MainActivity.this, 0, new Intent(DELIVERED), 0);
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}

/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
}

@Override
public void onLocationChanged(Location location) {
    boolean flag1 = true, flag2 = true;
    lat = (float) (location.getLatitude());
    lng = (float) (location.getLongitude());
    if (lng < 0) {
        lng = -lng;
        flag1 = false;
    }
    if (lat < 0) {
        lat = -lat;
        flag2 = false;
    }

    if (flag2)
        latituteField.setText("Latitude = " + lat + " N" + "\n");
    else
        latituteField.setText("Latitude = " + lat + " S" + "\n");

    if (flag1)
        longitudeField.setText("Longitude = " + lng + " E");
    else
        longitudeField.setText("Longitude = " + lng + " W");
    // deg = Math.abs((int) lat);
    // min = (int) ((lat - (float) deg) * 60.0);
    // sec = (int) ((((lat - (float) deg) * 60) - min) * 60);
    // if (flag2)
    // latituteField.setText("Latitude    =  " +String.valueOf(deg) + "° "
    // + String.valueOf(min) + "\' " + String.valueOf(sec) + "\""
    // + 'N'+"\n");
    // else
    // latituteField.setText("Latitude    =  " +String.valueOf(deg) + "° "
    // + String.valueOf(min) + "\' " + String.valueOf(sec) + "\""
    // + 'S'+"\n");
    // deg = Math.abs((int) lng);
    // min = (int) ((lng - (float) deg) * 60.0);
    // sec = (int) ((((lng - (float) deg) * 60) - min) * 60);
    // if (flag1)
    // longitudeField.setText("Longitude = " + String.valueOf(deg) + "° "
    // + String.valueOf(min) + "\' " + String.valueOf(sec) + "\""
    // + 'E');
    // else
    // longitudeField.setText("Longitude = " + String.valueOf(deg) + "° "
    // + String.valueOf(min) + "\' " + String.valueOf(sec) + "\""
    // + 'W');
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    Toast.makeText(this, "Enabled new provider " + provider,
            Toast.LENGTH_SHORT).show();

}

@Override
public void onProviderDisabled(String provider) {
    Toast.makeText(this, "Disabled provider " + provider,
            Toast.LENGTH_SHORT).show();
}

}


Solution

  • I had to add requestlocationupdates() to make it work Strange that I was getting GPS coordinates without using the said function on Jellybean and KitKat. So the modified code is :

    Criteria criteria = new Criteria();
    Location location = locationManager
    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
    provider = locationManager.getBestProvider(criteria, true);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
    MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);