Ahmed Safaa
Ahmed Safaa

Reputation: 21

Get the Location without internet

I try to get android current location without using internet and I create the code in below but does not display any value in TextView

package com.example.ahmed.gbs_location;

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

public class MainActivity extends AppCompatActivity implements LocationListener {
TextView latitude;
TextView longitude;

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

    latitude = (TextView) findViewById(R.id.tb1);
    longitude = (TextView) findViewById(R.id.tb2);
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(MainActivity.this, "Ahmed", Toast.LENGTH_SHORT).show();
        return;
    }
    else
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}

@Override
public void onLocationChanged(Location location) {
    latitude.setText(String.valueOf(d));
    longitude.setText(String.valueOf( location.getLongitude()));
    latitude.setText(String.valueOf(( location.getLatitude())));
}

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

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

}

In AndroidManifest.xml this code

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.ahmed.gbs_location">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<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>

I did not know why there are value for latitude and longitude display in TextView?

Upvotes: 0

Views: 1327

Answers (1)

Randheer
Randheer

Reputation: 1084

If your targetSdkVersion is >22, try requesting runtime permission & request location update from onRequestPermissionsResult.

For runtime permission click Here!

Also your code will only work if location services is enable in device setting. Since you are neither checking location services status nor requesting to enable it.

Try using FusedLocationProviderApi you don't have to use intent, check Here!

Upvotes: 1

Related Questions