Home>
I want to achieve

I am creating an app that outputs the latitude and longitude of my current position on the screen using Android Studio. However, although both build and execution are successful, the execution result is not reflected in the emulator.
In other words, the emulator will be blank. Notation such as LTE at the top of the screen appears.
I'm doing it while looking at a reference book, but it doesn't work.

Execution result of reference book

LocationEx>
Latitude: xxx.xxxxxxx
Longitude: yyy.yyyyyyy

Source code
{
     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));
                // Get location manager
                manager
                        = (LocationManager) getSystemService (Context.LOCATION_SERVICE);
                // Check user permission
                checkPermissions ();
            }
            // Called when activity resumes
    @Override
    public void onResume () {
        super.onResume ();
        // Start updating location information
        setLocationUpdateEnabled (true);
    }
    // Called when activity is paused
    @Overridepublic 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 (isGranted ()) {
            return;
        }
        // Register and unregister location manager
        try {
            if (enabled) {
                manager.requestLocationUpdates (
                        LocationManager.NETWORK_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.locationex">
  <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" />                    <intent-filter>                            <category android: name = "android.intent.category.LAUNCHER" />        </intent-filter>    </activity></application></manifest>
Supplement

The development environment is Android Studio 4.1, Windows 10, Android Gradle Plugin Ver4.1.0, Gradle Ver6.5.1, the emulator is Pixel 3a_API30_x86, and the language used is Java. If i have any other questions, please feel free to ask.

  • Answer # 1

    There are two mistakes.

    First of all, I am creating a layout, but I would like to create a LinearLayout, place a TextView in it and draw it in Activity, but in the code presented, the process of placing TextView in LinearLayout and that LinearLayout is Activity The process of drawing to is missing. This will display nothing.

          // 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);// Place TextView on LinearLayout
            setContentView (layout);// Draw LinearLayout on Activity

    Let's add two lines like this.

    then,

      // Start/stop location information update
        private void setLocationUpdateEnabled (boolean enabled) {
            // Check permissions
            if (isGranted ()) {
                return;
            }

    However, if this is the case, I think that "if you have the authority, you will immediately return". Then it is beyond thatrequestLocationUpdates ()Positioning will not start because the process does not proceed. Isn't it the other way around?


    And one more thing, if you're trying to work with an emulator, are you trying to send latitude/longitude information using the Extended controls Location? If so,requestLocationUpdates ()If you do not specify the provider specified in GPS for GPSonLocationChanged ()I don't think it will be called.

                  manager.requestLocationUpdates (
                            LocationManager.GPS_PROVIDER, 0, 0, this);


    like,LocationManager.GPS_PROVIDERWill need to be set to.