2016-06-19 29 views
0

Ich habe eine App erstellt, die den aktuellen GPS-Standort an gespeicherte Nummern sendet. Aber der Standort war immer 0,0, 0,0.GPS-Standort wird immer als 0.0, 0.0 (LocationListener implementiert) angezeigt

Hier ist der Code:

public class ContactsList extends AppCompatActivity { 

Button addContacts; 
Button emergencyButton; 

ListView listView; 

ArrayList<HashMap<String, String>> contactsList; 
ArrayList<Contact> contactobjectsList; 
ArrayList<String> phonenumbersList; 

DatabaseHelper databaseHelper = new DatabaseHelper(ContactsList.this); 

LocationManager locationManager; 
LocationListener locationListener; 

double latitudeVal, longitudeVal; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_contacts_list); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    addContacts = (Button) findViewById(R.id.moveButton); 
    listView = (ListView) findViewById(R.id.contactslistView); 
    emergencyButton = (Button) findViewById(R.id.emergency_button); 

    contactobjectsList = new ArrayList<Contact>(); 
    contactobjectsList = databaseHelper.getContacts(); 

    phonenumbersList = new ArrayList<String>(); 
    phonenumbersList = databaseHelper.getNumbers(); 

    contactsList = new ArrayList<HashMap<String, String>>(); 
    contactsList = databaseHelper.getValues(); 

    // For GPS: 

    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 
    locationListener = new LocationListener() { 
     @Override 
     public void onLocationChanged(Location location) { 
      if (location != null) {     

       latitudeVal = location.getLatitude(); 
       longitudeVal = location.getLongitude(); 
      } 
     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 

     } 

     @Override 
     public void onProviderEnabled(String provider) { 
      Toast toast = Toast.makeText(getApplicationContext(), "GPS Enabled", Toast.LENGTH_LONG); 
      toast.show(); 


     } 

     @Override 
     public void onProviderDisabled(String provider) { 

      Toast toast = Toast.makeText(getApplicationContext(), "Enable GPS", Toast.LENGTH_LONG); 
      toast.show(); 
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      startActivity(intent); 


     } 
    }; 


    ListAdapter listAdapter = new SimpleAdapter(ContactsList.this, contactsList, R.layout.customlayout, 
      new String[]{DatabaseHelper.COLUMN_ID, DatabaseHelper.COLUMN_PERSONNAME, DatabaseHelper.COLUMN_PHONENUMBER}, 
      new int[]{R.id._idcust, R.id.personnamecust, R.id.phonenumbercust}); 

    listView.setAdapter(listAdapter); 


    addContacts.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      Intent move = new Intent(ContactsList.this, AddContact.class); 
      startActivity(move); 


     } 
    }); 


    emergencyButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, locationListener); 



      String printLat = Double.toString(latitudeVal); 
      String printLon = Double.toString(longitudeVal); 

      Toast toast = Toast.makeText(getApplicationContext(),printLat+", "+printLon,Toast.LENGTH_LONG); 
      toast.show(); 

      // sendSMS(phonenumbersList); 


     } 
    }); 



} 

Aus irgendeinem Grunde Standardwerte für die Breite und die Länge wie in der Location angegebene Klasse sind die, die zurückgegeben werden (0.0, 0.0). Ich weiß wirklich nicht, was mit meinem Code nicht stimmt, und ich würde an dieser Stelle ernsthaft Hilfe schätzen.

Antwort

0

sollten Sie diese Zeile hinzufügen

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, locationListener); 

gerade nach dieser Zeile

locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

statt in emergencyButton.setOnClickListener

+0

Es hilft nicht –

+0

das kann Ihnen helfen http://stackoverflow.com/questions/17519198/how-to-get-the-current-location-latitude-and-longitude-in-android –

1
<uses-permission 
    android:name="android.permission.ACCESS_FINE_LOCATION" 
    android:maxSdkVersion="22" /> 

entfernen android: maxSdkVersion = "22" von Manifest, löste das Problem oder wenn Sie targetSdkVersion> 21 dann müssen Sie Laufzeit Berechtigung für ACCESS_FINE_LOCATION implementieren.