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