2013-10-12 3 views
0

GPS 또는 네트워크 공급자를 통해 현재 위치를 얻으려면 인터넷 사용 권한이 정말로 필요합니까?위치 업데이트를위한 인터넷 권한

인터넷 권한을 부여하지 않고 아래 코드를 실행합니다. 정상적으로 작동합니다.

의 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; 
    } 

} 
+0

질문에 답변하지 못 했습니까? – Kuffs

+0

아니요, 그는 적어도 하나의 Android 버전에서 작동 함을 확인했습니다. 아마도 그는 모든 것이 동일하게 적용되는지 여부를 알고 싶어합니다. 이는 전혀 다른 문제이며 실험적으로 쉽게 답변 할 수있는 문제가 아닙니다. – Jules

답변

1

네, 위치 권한을 추가하고 개발자 사이트에서 이것을 볼 때도 작동합니다 http://developer.android.com/training/location/retrieve-current.html

+0

그래서 현재 위치를 얻으려면 인터넷 사용 권한을 부여 할 필요가 없습니까? 그러나 나는 빨간 블로그를 가지고있다. 그들은 인터넷 허가를 요청하고 있습니다. 내가 왜 몰라? http : //www.androidhive.info/2012/07/android-gps-location-manager-tutorial/, http://www.vogella.com/articles/AndroidLocationAPI/article.html – saravanan

+0

또한 왜 내가 Inbuilt지도 응용 프로그램은 "데이터 연결은 Unavailale"오류를 제공합니다 내가 인터넷 연결을 비활성화하면 – saravanan

+0

@ saravanan는 ACCESS_FINE_LOCATION 대신 ACCESS_COARSE_LOCATION에 대한 권한을 변경하고 응용 프로그램을 실행하고 무슨 일이 일어 났는지 알려주시겠습니까? –

관련 문제