2017-05-03 2 views
0

저는 자바 프로그래밍과 Android 프로그래밍에서 새로운데, 내 위치를 얻으려고하고 있지만 몇 가지 문제가 있습니다. Android에서 위치를 가져 오는 데 문제가 발생했습니다.

내가

는 코드

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details. 
      return; 
     } 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 60, 10, this); 
의이 부분에서 경우 문장에 도달했을 때 나는 그것이 충돌 디버깅에 의해 발견

public class MiServicio extends Service implements LocationListener{private final Context context; 
double latitud; 
double longitud; 
Location location; 
boolean gpsActivo; 
TextView texto; 
LocationManager locationManager; 


public MiServicio() { 
    super(); 
    this.context = this.getApplicationContext(); 
} 

public MiServicio(Context c) { 
    super(); 
    this.context = c; 
    getLocation(); 
} 

public void setView(View v) { 
    texto = (TextView) v; 
    texto.setText("Coordenadas: " + latitud + ", " + longitud); 
} 

public void getLocation() { 
    try { 
     locationManager = (LocationManager) this.context.getSystemService(LOCATION_SERVICE); 
     gpsActivo = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    } catch (Exception ignored) { 
    } 

    if (gpsActivo) { 


     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      //           int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details. 
      return; 
     } 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 60, 10, this); 

     location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     latitud = location.getLatitude(); 
     longitud = location.getLongitude(); 
    } 
} 

@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

@Override 
public void onLocationChanged(Location location) { 

} 

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

} 

@Override 
public void onProviderEnabled(String provider) { 

} 

@Override 
public void onProviderDisabled(String provider) { 

} 

}을 작성한 모든 코드

누군가이 문제를 해결하는 방법을 설명 할 수 있습니까?

감사합니다.

또한 여기에 내가 매니페스트

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> 

편집에서 설정 한 권한

These are all the errors I got

는 또한 내가

+0

어떤 오류가 있습니까? logcat – tahsinRupam

+0

에뮬레이터 란 무엇입니까? 또는 장치 위치가 켜져 있습니까? –

+0

[이 링크] (http://stackoverflow.com/questions/37322645/nullpointerexception-when-trying-to-check-permissions)를 참조하십시오. 귀하의 문제를 해결하는 데 도움이되기를 바랍니다. –

답변

0

하려고이 응용 프로그램을 테스트하기 위해 내 전화를 사용할 수 있습니다 if() 문의 다음 코드를 사용하십시오.

,
if (context.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
0

가 친절하게 확인하고 서비스를 만들 때 코드

 int permissionCheck = ContextCompat.checkSelfPermission(this, 
       Manifest.permission.ACCESS_FINE_LOCATION); 
     if (permissionCheck != PackageManager.PERMISSION_GRANTED) { 
      checkPermission(); 
     } 



private void checkPermission() { 
    // Here, thisActivity is the current activity 
    if (ContextCompat.checkSelfPermission(this, 
      Manifest.permission.ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 


     // Should we show an explanation? 
     if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
       Manifest.permission.ACCESS_FINE_LOCATION)) { 
      // Show an explanation to the user asynchronously -- don't block 
      // this thread waiting for the user's response! After the user 
      // sees the explanation, try again to request the permission. 
      return; 
     } else { 
      // No explanation needed, we can request the permission. 
      ActivityCompat.requestPermissions(this, 
        new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
        MY_PERMISSIONS_REQUEST_LOCATION); 
      // MY_PERMISSIONS_REQUEST_READ_PHONE_STATE is an 
      // app-defined int constant. The callback method gets the 
      // result of the request. 
      return; 
     } 
    } 
} 
0

아래에 의해 권한을 부여하기위한 코드를 교체, 당신은의 생성자를 호출하지 않고 당신은 그것을 무시 않습니다.

context.startService(new Intent(context, MiServicio.class)); 

컨텍스트가 안드로이드에 의해 구현 및 매니페스트에 선언해야하는 대신 활동에 MiServicio servicio = new MiServicio(Context c)를 사용하여 어디든지이 서비스를 시작하려고의 다음 명령을 사용합니다. Android 서비스 사용 방법에 대한 전체 자습서는 Android Services tutorial을 확인하십시오.

관련 문제