Toast is not displayed with GPS position

Asked

Viewed 79 times

1

I am developing a very simple app, when the app runs it opens a TOAST with my GPS position. The app is running normally, but TOAST is not showing. What may be wrong ?

Androidmanifest.xml

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <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>
    </application>

</manifest>

Mainactivity.java

package com.teste.masterpoint;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.provider.Settings;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity implements LocationListener {

    private LocationManager locationManager;

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

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

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                2000, 1, this);

    }

    @Override
    public void onLocationChanged(Location location) {

        String msg = "New Latitude: " + location.getLatitude() + "New Longitude: " + location.getLongitude();

        Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onProviderDisabled(String provider) {

        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
        Toast.makeText(getBaseContext(), "Gps is turned off!! ",
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderEnabled(String provider) {

        Toast.makeText(getBaseContext(), "Gps is turned on!! ",
                Toast.LENGTH_SHORT).show();
    }

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

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

2 answers

1

The Toast is within the method onLocationChanged(), as its name indicates is only called when there is a change of its location.(1)
As long as there is no such change, within the limits set out in locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 1, this);, the method is not called.

Before this happens it can by using the method locationManager .getLastKnownLocation() get your last known location.

In the method onCreate() include the following code:

if (locationManager != null) {
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (location != null) {
        String msg = "New Latitude: " + location.getLatitude() + "New Longitude: " + location.getLongitude();

        Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
    }
}

(1)Of course this method will also only be called if the GPS is connected and with access to satellites.

  • Appeared the following error ( Cannot resolve Symbol 'Location' ).

  • I edited the answer.

  • I did as you said, but Toast still doesn’t show up.

  • You added the permission to use GPS?

  • My manifest is as I put here in question.

  • Missing some permission ?

  • No. I don’t know what else to say. The reasons for not showing Toast are: locationManager = null or location = null, see which one it is. Has the GPS on?

Show 2 more comments

0

Step 1: Have you checked whether the onLocationChanged() is being called? Use Log to verify this:

Log.v("QualquerTAG", "Entrou no onLocationChanged() Latitude: "+location.getLatitude()+" | Longitude: "+location.getLongitude());

and check if it was called on the console.

Step 2: If the method is being called (step 1), it is likely that it is not running on the Thread UI. Then try calling Toast inside the Thread UI:

MainActivity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
        }
    });

PS: I strongly advise not to use getBaseContext() as the context of Toast. I advise using MainActivity.this instead.

Step 3: If nothing works, try adding these permissions and see if the problem is solved:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.