0

I am working on an app that requires the android device to know his current location. More exactly the latitude and longitude. Here is my LocationListener. The latitude and longitude variables are 2 double variables. What must I do further?

private final LocationListener mLocationListener = new LocationListener() {
    @Override
    public void onLocationChanged(final Location location) {
        latitude = location.getLatitude();
        longitude = location.getLongitude();
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }
};

I have already read this question's answer (How to get current location in Android) and here is where I am stuck.

PS: Please no tutorials with GPSTracker as answer. I tried that and it is not working.

////////////////////////////////////////////////////////////////////////////////

Here is the program that I am running. In stead of textView form your example I used Logs.

public class MainActivity extends Activity {

int myLatitude, myLongitude;
private static final String TAG = MainActivity.class.getSimpleName();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //retrieve a reference to an instance of TelephonyManager
    TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    GsmCellLocation cellLocation;
    cellLocation = (GsmCellLocation) telephonyManager.getCellLocation();

    int cid = cellLocation.getCid();
    int lac = cellLocation.getLac();
    if(RqsLocation(cid , lac))
    {
        Log.e(TAG , myLatitude+"");
        Log.e(TAG , myLongitude+"");
    }
    else
    {
        Log.e(TAG , myLatitude+"");
        Log.e(TAG , myLongitude+"");
    }




}

private Boolean RqsLocation(int cid, int lac){

    Boolean result = false;

    String urlmmap = "http://www.google.com/glm/mmap";

    try {
        URL url = new URL(urlmmap);
        URLConnection conn = url.openConnection();
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setRequestMethod("POST");
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);
        httpConn.connect();

        OutputStream outputStream = httpConn.getOutputStream();
        WriteData(outputStream, cid, lac);

        InputStream inputStream = httpConn.getInputStream();
        DataInputStream dataInputStream = new DataInputStream(inputStream);

        dataInputStream.readShort();
        dataInputStream.readByte();
        int code = dataInputStream.readInt();
        if (code == 0) {
            myLatitude = dataInputStream.readInt();
            myLongitude = dataInputStream.readInt();

            result = true;

        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return result;

}

private void WriteData(OutputStream out, int cid, int lac)
        throws IOException
{
    DataOutputStream dataOutputStream = new DataOutputStream(out);
    dataOutputStream.writeShort(21);
    dataOutputStream.writeLong(0);
    dataOutputStream.writeUTF("en");
    dataOutputStream.writeUTF("Android");
    dataOutputStream.writeUTF("1.0");
    dataOutputStream.writeUTF("Web");
    dataOutputStream.writeByte(27);
    dataOutputStream.writeInt(0);
    dataOutputStream.writeInt(0);
    dataOutputStream.writeInt(3);
    dataOutputStream.writeUTF("");

    dataOutputStream.writeInt(cid);
    dataOutputStream.writeInt(lac);

    dataOutputStream.writeInt(0);
    dataOutputStream.writeInt(0);
    dataOutputStream.writeInt(0);
    dataOutputStream.writeInt(0);
    dataOutputStream.flush();
}

}

Here is my manifest file:

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

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    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>

2 Answers2

0

Convert cellLocation to real location (Latitude and Longitude), using 1."http://www.google.com/glm/mmap"

2.Refer this document

Pallavi Tapkir
  • 781
  • 7
  • 28
  • No error but my application is not working properly. I mean, when it reaches the point to get the location, it stops working. –  Jan 31 '18 at 14:56
  • Application not working properly means exactly what is happening? Share error log – Pallavi Tapkir Feb 01 '18 at 05:26
  • It is showing no error. But when I start the activity, there is nothing showing. Just a white screen. Before I added this code, it showed a `webview`. –  Feb 01 '18 at 11:57
  • please share your code snippet for fetching latitude and longitude, and what action you have taken for webview. – Pallavi Tapkir Feb 01 '18 at 12:15
  • I include in the question body the code that you sent me. I made a new project and tried the code there. Please do the same and put on your xml file anything. A `textview`, an `editText` and tell me if it appears on the screen. –  Feb 01 '18 at 12:27
  • There is an error on one line and you have 2 options: 1 to Suppers the missing permission and there for the program will not run. And 2 to add the permission checked witch will result in a running program that dose nothing and showing a white schreen. –  Feb 01 '18 at 12:34
0

See this api call. It works fine.

private void fetchOffLineLocation() {
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        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;
        }

        if (telephonyManager != null) {
            GsmCellLocation cellLocation = (GsmCellLocation) telephonyManager.getCellLocation();

            int cid = cellLocation.getCid();
            int lac = cellLocation.getLac();

            getRqsLocation(cid, lac)
                    .filter(aBoolean -> aBoolean && latitude != 0 && longitude != 0)
                    .flatMap(aBoolean -> getSunriseSunset(latitude, longitude, Utility.getCurrentDateAndTime("yyyy-MM-dd")))
                    .filter(response-> response!= null && response.getResults() != null)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(response-> {

                        sSunrise = response.getResults().getSunrise();
                        sSunSet = response.getResults().getSunset();
                        LogUtility.d("Sunrise time " + sSunrise);
                        LogUtility.d("Sunset time " + sSunSet);

                    }, throwable -> LogUtility.e(throwable.getMessage()));
        } else {
            LogUtility.e("Telephony manager null");
        }
    }
Pallavi Tapkir
  • 781
  • 7
  • 28