1

我正在使用日食。源代码如下:当我在各个地方单击第一个按钮时,结果是一样的。请帮忙..提前谢谢

GPSSurvey Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vinod.gpssurvey"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/> 

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.vinod.gpssurvey.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

MainActivity.java

package com.vinod.gpssurvey;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
Button buttonSend;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    buttonSend = (Button) findViewById(R.id.Firstbutton);
    buttonSend.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
             Criteria criteria = new Criteria();
             String bestProvider = locationManager.getBestProvider(criteria, false);
             //locationManager.requestLocationUpdates(bestProvider, 400, 1, this);
             Location location = locationManager.getLastKnownLocation(bestProvider);

             Double lat,lon;
             try {
               lat = location.getLatitude ();
               lon = location.getLongitude ();

               TextView firstLat = (TextView)findViewById(R.id.FirstLat);
               TextView firstLong = (TextView)findViewById(R.id.FirstLong);
                firstLat.setText(lat.toString());
                firstLong.setText(lon.toString());

                String Text = "My current location is: " +
                        "Latitud = " + location.getLatitude() +
                        "Longitud = " + location.getLongitude();
                        Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();

             }
             catch (NullPointerException e){
                 e.printStackTrace();

             }
        }

    });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/FirstLong"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="First Longitute : " />


    <TextView
    android:id="@+id/FirstLat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="First Latitute : " />

<Button
    android:id="@+id/Firstbutton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="First Corner" />


<TextView
    android:id="@+id/SecLong"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Sec Longitute : " />

    <TextView
    android:id="@+id/SecLat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Sec Latitute : "  />

<Button
    android:id="@+id/Firstbutton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Second Corner" />


<TextView
    android:id="@+id/ThirdLong"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Third Longitute : " />

    <TextView
    android:id="@+id/ThirdLat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Third Latitute : " />

<Button
    android:id="@+id/Thirdbutton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Third Corner" />

<TextView
    android:id="@+id/FourthLong"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Fourth Longitute : " />

    <TextView
    android:id="@+id/FourthLat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Fourth Latitute : " />

<Button
    android:id="@+id/Fourthtbutton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Fourth Corner" />


<TextView
    android:id="@+id/FifthLong"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Fifth Longitute : " />

    <TextView
    android:id="@+id/FifthtLat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Fifth Latitute : " />

<Button
    android:id="@+id/Fifthbutton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Fifth Corner" />




</LinearLayout>
4

1 回答 1

0

当 LocationProvider(通常是 GPS 或 WiFi)处于活动状态并实际检测到位置时,调用 会getLastKnownLocation为您提供检测到的最后位置。

根据您的设备,GPS 提供商可能需要一段时间(例如 1 分钟或更长时间)才能提供第一个位置。您可以在导航栏上看到连接到 GPS 卫星的状态(同样,取决于您使用的设备)

如果您在室内,它可能永远不会收到任何位置。

你的代码对我来说看起来不错。

于 2013-09-09T13:28:04.813 回答