试图在Android手机上获取GPS数据

时间:2013-04-16 05:07:07

标签: android gps

我是Android编程的新手并且卡住了。在我的程序中,我试图访问gps并在textviews中显示纬度,经度等。当我使用模拟器和telnet连接在计算机上运行我的代码时,我能够看到模拟器上显示的数据。但是,当我在Android设备(HTC ADR6300手机)上运行此程序时,我的所有文本视图都是空的,没有数据显示。我只看到标签,但没有数据。我不确定我做错了什么。感谢您的帮助

这是MainActivity.java

 public class MainActivity extends Activity {
    TextView tvLong;
    TextView tvLat;
    TextView tvSpeed;
    TextView tvAlt;
    TextView tvTime;
    TextView tvProvider;
    String ProviderName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            // Get LocationManager to access the GPS data on your phone
            LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            // Who is the data provider? Built-in GPS receiver
            LocationProvider provider = lm.getProvider("gps");

            // Receive location updates from GPS receiver every minute, every 10m
            // Read the data using locationListener
            lm.requestLocationUpdates("gps", 60000, 1, locationListener);
            ProviderName = provider.getName();  
    }


    /** The LocationListener shows the most recent data with every call of onLocationChanged()() */
    private final LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location loc) {
                    setContentView(R.layout.activity_main);
                    tvProvider = (TextView)findViewById(R.id.ProviderText);
                    tvLong = (TextView)findViewById(R.id.LonText);
                    tvLat = (TextView)findViewById(R.id.LatText);
                    tvSpeed = (TextView)findViewById(R.id.SpeedText);
                    tvAlt = (TextView)findViewById(R.id.AltText);
                    tvTime = (TextView)findViewById(R.id.TimeText);

                    tvProvider.setText(ProviderName);
                    tvLong.setText(Double.toString(loc.getLongitude()));
                    tvLat.setText(Double.toString(loc.getLatitude()));
                    tvSpeed.setText(Float.toString(loc.getSpeed()));
                    tvAlt.setText(Double.toString(loc.getAltitude()));
                    Date date = new Date(loc.getTime());
                    DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
                    tvTime.setText(dateFormat.format(date));
            }
            //Other methods of LocationListener are not needed, so add empty methods
            public void onProviderDisabled(String provider){}
            public void onProviderEnabled(String provider){}
            public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    @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;
    }
    public void onExitMenu(MenuItem menuItem)
    {
            finish();
    }
}    

这是我的activity_main.xml文件。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/provider" />

<TextView
    android:id="@+id/ProviderText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#0f0" />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/lat" />

<TextView
    android:id="@+id/LatText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#0f0"
    android:textSize="40.0sp" />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/lon" />

<TextView
    android:id="@+id/LonText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#0f0"
    android:textSize="40.0sp" />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/speed" />

<TextView
    android:id="@+id/SpeedText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#0f0"
    android:textSize="40.0sp" />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/alt" />

<TextView
    android:id="@+id/AltText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#0f0"
    android:textSize="40.0sp" />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/time" />

<TextView
    android:id="@+id/TimeText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#0f0"
    android:textSize="40.0sp" />

</LinearLayout>  

这是我的AndroidManifest文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gps"
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>
<uses-permission android:name="android.permission.INTERNET" >
    </uses-permission>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.gps.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>

0 个答案:

没有答案
相关问题