0

I was looking for the solution to my problem the whole day now, but I couldn't find out. I'm absolutely new to java/android studio/app programming and probably it's just a very small thing that I don't see.

I just want my App to show the current location of the device. Nothing more.

When running the App on the emulator, no error is shown and the App starts.

Then I open the extended controls and push the "SEND"-button in "Location". Now I would expect my App to show the current location, but nothing happens. The TextViews "textLat", "textLong" and "textAlt" are staying empty.

I have absolutely no Idea what my fault is. I wrote that code with the help of different tutorials. I also tried it on my real device.

I would be very grateful for any help! Thanks alot!

    package com.example.findlocation_test2;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    TextView textLat;
    TextView textLong;
    TextView textAlt;


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

        textLat = (TextView) findViewById(R.id.textLat);
        textLong = (TextView) findViewById(R.id.textLong);
        textAlt = (TextView) findViewById(R.id.textAlt);

        LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {

                if(location != null) {
                    double dLat = location.getLatitude();
                    double dLong = location.getLongitude();
                    double dAlt = location.getAltitude();

                    textLat.setText(Double.toString(dLat));
                    textLong.setText(Double.toString(dLong));
                    textAlt.setText(Double.toString(dAlt));
                }
                else {
                    textLat.setText("Fehler");
                    textLong.setText("Fehler");
                    textAlt.setText("Fehler");
                }
            }

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

            }

            @Override
            public void onProviderEnabled(String provider) {

                Toast.makeText(getBaseContext(), "GPS wurde akiviert",
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onProviderDisabled(String provider) {

                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
                Toast.makeText(getBaseContext(), "GPS wurde deaktiviert",
                        Toast.LENGTH_SHORT).show();

            }
        };

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }

        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0, ll);

    }

}

And in the manifest I added the permissions:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.findlocation_test2">

    <uses-permission android:name="android.permission.ACCESS_INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> /**Erlaubnis, um auf die GPS Daten zuzugreifen*/
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Look at the results within this: https://stackoverflow.com/questions/17591147/how-to-get-current-location-in-android it explains how to do it in the answer. If your code is the same you are likely not creating your buttons/events properly. – Hesein Burg May 24 '17 at 17:09
  • Hey, I already tried that and like most of the peoples commenting there I only get 0.0 coordinates. Plus I dont need such a complicated code. Actually I just want to find out the coordinates from the system so that I can work with them. I thought it would be an easy task... – Standroid May 25 '17 at 10:46

0 Answers0