简体   繁体   中英

How to call a Location method from a button click in Java Android?

pretty new to java but after tutorials and such, I understand the basics. I'm trying to call a method I created to find the location of my device and then append the strings to a textview. How do I call this method from my button click? I tried just putting all the code inside my button so it would just run when clicked, but it doesn't like that. Does a button click have to contain a View parameter or because when I use public onLocationChanged(Location location) I get an error saying that it can't find the public onLocationChanged(View) . I tried doing public onLocationChanged(View view, Location location) but I get the same error in logcat. Thanks in advance for any help or feedback!

Edit: I saw on the android dev site "Now, when a user clicks the button, the Android system calls the activity's selfDestruct(View) method. In order for this to work, the method must be public and accept a View as its only parameter." when referring to the onClick parameter in the xml file. If I don't use this parameter and instead use the onClickListener method in the java file, can I use Location?


XML FILE

<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/GPS"
    android:id="@+id/vid_message_button"
    android:onCLick="onLocationChanged" />

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

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);

}



public void onLocationChanged(Location location) {


    if (view.getId() == R.id.send_GPS_button) {

    double lat = (double) (location.getLatitude());
    double lng = (double) (location.getLongitude());
    float accuracy = (float) (location.getAccuracy());
    double alt = (double) (location.getAltitude());
    double speed = (double) (location.getSpeed());
    double heading = (double) (location.getBearing());

    messageLogTextView.append("Latitude: " + String.valueOf(lat) + "\n" +
                    "Longitude: " + String.valueOf(longi) + "\n" +
                    "Accuracy: " + String.valueOf(accuracy) + "\n" +
                    "Altitude: " + String.valueOf(alt) + "\n" +
                    "Speed: " + String.valueOf(speed) + "\n" +
                    "Heading: " + String.valueOf(heading) + "\n");

    }    

}

public void onSendGPSButtonClick(View view) {

}

Assuming that you have mentioned onLocationChanged for the android:onclick attribute of your Button's xml file, in your activity class, have the method with the signature onLocationChanged(View view).

Inside, this method, you can have

if (view.getId() == R.id.yourButtonName) {

//Add your above code for the location and setting the location to your textview

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM