位置更新的Internet权限

时间:2013-10-12 18:00:11

标签: android location

我是否真的需要互联网许可才能通过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>

LocationService.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;
    }

}

1 个答案:

答案 0 :(得分:1)

是的,它会在您添加位置权限时起作用,并且也会在开发者网站http://developer.android.com/training/location/retrieve-current.html

上看到