Home>

I am developing an Android application that outputs the current position in latitude and longitude, but the output result will be the same latitude and longitude no matter where I do it. In other words, it is no longer in its current position. When I looked up the output latitude and longitude on Google Map, I found a pin in China. I would appreciate it if anyone could understand.

Corresponding source code
package net.npaka.locationexx;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
// Get location information
public class LocationEx extends Activity implements LocationListener {
    public final static String BR = System.getProperty ("line.separator");
    private final static int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
    private final static String [] PERMISSIONS = {
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION};
    private final static int REQUEST_PERMISSION = 1;
    private TextView textView;// Textview
    private LocationManager manager;
    @Override
    public void onCreate (Bundle bundle) {
        super.onCreate (bundle);
        requestWindowFeature (Window.FEATURE_NO_TITLE);
        // Create layout
        LinearLayout layout = new LinearLayout (this);
        layout.setBackgroundColor (Color.WHITE);
        layout.setOrientation (LinearLayout.VERTICAL);
        // Generate text view
        textView = new TextView (this);
        textView.setText ("LocationEx");
        textView.setTextSize (24);
        textView.setTextColor (Color.BLACK);
        textView.setLayoutParams (new LinearLayout.LayoutParams (WC, WC));
        layout.addView (textView);// Add
        setContentView (layout);// Add
        // Get location manager
        manager
                = (LocationManager) getSystemService (Context.LOCATION_SERVICE);
        // Check user permission
        checkPermissions ();
    }
    // Called when activity resumes
    @Overridepublic void onResume () {
        super.onResume ();
        // Start updating location information
        setLocationUpdateEnabled (true);
    }
    // Called when activity is paused
    @Override
    public void onPause () {
        super.onPause ();
        // Start updating location information
        setLocationUpdateEnabled (false);
    }
    // Called when notifying location information
    public void onLocationChanged (Location location) {
        // Get latitude and longitude
        textView.setText (String.format ("LocationEx>% s latitude:% s% s longitude:% s", BR, location.getLatitude (), BR, location.getLongitude ()));
    }
    // Called when notifying location information acquisition activation
    public void onProviderEnabled (String provider) {
    }
    // Called when notifying location information acquisition invalidation
    public void onProviderDisabled (String provider) {
    }
    // Start/stop location information update
    private void setLocationUpdateEnabled (boolean enabled) {
        // Check permissions
        if (ActivityCompat.checkSelfPermission (this, Manifest.permission.ACCESS_FINE_LOCATION)! = PackageManager.PERMISSION_GRANTED&&ActivityCompat.checkSelfPermission (this, Manifest.permission.ACCESS_COARSE_LOCATION)! = PackageManager.PERMISSION_GRANTED) {
            return;
        }
        if (! isGranted ()) {
            return;
        }
        // Register and unregister location manager
        try {
            if (enabled) {
                manager.requestLocationUpdates (
                        LocationManager.GPS_PROVIDER, 0, 0, this);
            } else {
                manager.removeUpdates (this);
            }
        } catch (SecurityException e) {
            e.printStackTrace ();
        }
    }
    // Check user permission
    private void checkPermissions () {
        // Not allowed
        if (! isGranted ()) {
            // Display permission dialog
            ActivityCompat.requestPermissions (this, PERMISSIONS,
                    REQUEST_PERMISSION);
        }
    }// Get the user's permission
    private boolean isGranted ()
    {
        for (String permission: PERMISSIONS) {
            if (ContextCompat.checkSelfPermission (LocationEx.this, permission)! =
                    PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }
    // Called when the permission dialog is selected
    @Override
    public void onRequestPermissionsResult (int requestCode,
                                           String [] permissions, int [] results) {
        if (requestCode == REQUEST_PERMISSION) {
            // Not allowed
            if (! isGranted ()) textView.setText (String.format ("LocationEx>% s Location acquisition is not permitted.", BR));
        } else {
            super.onRequestPermissionsResult (requestCode, permissions, results);
        }
    }
}
<? xml version = "1.0" encoding = "utf-8"?><manifest xmlns: android = "http://schemas.android.com/apk/res/android"
package = "net.npaka.locationexx"><uses-permission android: name = "android.permission.INTERNET" /><uses-permission android: name = "android.permission.ACCESS_COARSE_LOCATION" /><uses-permission android: name = "android.permission.ACCESS_FINE_LOCATION" /><uses-permission android: name = "android.permission.MOCK_ACCESS_LOCATION" />        <intent-filter>                    <category android: name = "android.intent.category.LAUNCHER" />    </intent-filter></activity></application></manifest>
What I tried

I thought it was a temporary bug and tried it in two cities, but the same latitude and longitude were output.

Supplementary information

The development environment is Android Studio 4.1, Windows 10, Android Gradle Plugin Ver4.1.0, Gradle Ver6.5.1, the emulator is Pixel 4_API30, and the language used is Java. If i have any other questions, we will answer them.