如何只使用GPS在android中获取没有互联网的位置

时间:2011-05-16 05:30:59

标签: android

我想只使用GPS获取位置。我不想在这个应用程序中使用互联网和GPRS。我的代码如下;告诉我这里我错了。

代码:

package com.getlocation;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class UseGps extends Activity {
    /** Called when the activity is first created. */
    private String provider;
    LocationManager locationManager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); /*
                                         * Use the LocationManager class to
                                         * obtain GPS locations
                                         */
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener mlocListener = new MyLocationListener();
         Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            provider = locationManager.getBestProvider(criteria, true);
            locationManager.requestLocationUpdates(provider, 61000, 250,
                mlocListener);
    } /* Class My Location Listener */
public class MyLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(Location loc) {
        loc.getLatitude();
        loc.getLongitude();
        String Text = "My current location is: " + "Latitude = "
                + loc.getLatitude() + "Longitude = " + loc.getLongitude();
        Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT)
                .show();
        Log.d("TAG", "Starting..");
    }

    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(getApplicationContext(), "Gps Disabled",
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(getApplicationContext(), "Gps Enabled",
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
}/* End of Class MyLocationListener */
}/* End of UseGps Activity */

2 个答案:

答案 0 :(得分:3)

仅用于GPS提供商,它不需要GPRS。

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

您需要将权限放在清单文件中。

答案 1 :(得分:1)

您无需连接互联网即可在手机中运行GPS系统。 GPS时间同步不需要Internet连接。但是,如果您想在谷歌地图上显示当前位置,则可能需要连接互联网。

来你的代码对我来说一切都很好。

在您的活动中尝试此代码。

LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

LocationListener mlocListener = new YourLocationListener(getApplicationContext(), mobileNo, deviceId);
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,mlocListener);

并将其包含在androidmanifest.xml中

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />