0

我是否真的需要 Internet 权限才能通过 GPS 或网络提供商获取当前位置。

我在没有授予 Internet 权限的情况下运行以下代码。它工作正常。

AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

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

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

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

        <receiver android:name="com.example.practice.AlarmReceiver" >
        </receiver>


    </application>

</manifest>

定位服务.java

package com.example.location;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

public class LocationService implements LocationListener {

    private final Context mContext;
    private boolean isGPSEnabled = false;
    private boolean isNetworkEnabled = false;
    private boolean canGetLocation = false;

    private Location location;
    private double latitude;
    private double longitude;

    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;

    protected LocationManager locationManager;

    public LocationService(Context context) {
        this.mContext = context;
        location = getLocation();
    }

    public Location getLocation() {

        try {

            locationManager = (LocationManager) mContext
                    .getSystemService(Context.LOCATION_SERVICE);

            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER); 


            Log.i("network ", isNetworkEnabled+"");
            Log.i("GPS ", isGPSEnabled+""); 

            if (!isGPSEnabled && !isNetworkEnabled) {

            } else {
                canGetLocation = true;
                if (isGPSEnabled) {
                    if (locationManager != null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.i("GPS", "GPS");
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }

                    }
                }
                    if (isNetworkEnabled) {
                        Log.i("location", location+"");
                        if (location == null) {
                            Log.i("Location Manager", locationManager+""); 
                            if (locationManager != null) {
                                locationManager.requestLocationUpdates(
                                        LocationManager.NETWORK_PROVIDER,
                                        MIN_TIME_BW_UPDATES,
                                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                                Log.i("NETWORK", "NETWORK");

                                location = locationManager
                                        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

                                if (location != null) {
                                    latitude = location.getLatitude();
                                    longitude = location.getLongitude();
                                }
                            }
                        }
                    }
                }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return location;
    }

    public boolean canGetLocation() {
        return canGetLocation;
    }

    public void onLocationChanged(Location location) {

    }

    public void onProviderEnabled(String provider) {

    }

    public void onProviderDisabled(String provider) {
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

}

LocationActivity.java

package com.example.practice;

import com.example.location.LocationService;

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

public class LocationActivity extends Activity {

    LocationService locationService;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);

        button=(Button)findViewById(R.id.btn_location);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
              locationService=new LocationService(LocationActivity.this);
              if(locationService.canGetLocation())
              {
                  Location location=locationService.getLocation();
                  Toast.makeText(LocationActivity.this, location.getLatitude()+ "  "+location.getLongitude(),Toast.LENGTH_LONG).show();


              }
            }  
        });


    }

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

}
4

1 回答 1

1

是的,它会在您添加位置权限时起作用,并且还可以在开发人员网站http://developer.android.com/training/location/retrieve-current.html上看到这一点

于 2013-10-12T18:09:23.203 回答