2017-05-03 4 views
0

私はjavaでのプログラミングや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

の下で許可を与えるためにあなたのコードを置き換え、あなたはそれのコンストラクタを呼び出すことはありませんもあなたはそれを上書きします。代わりに、あなたの活動にMiServicio servicio = new MiServicio(Context c)を使用するか、またはあなたがこのサービスを開始しようとしているどこで、次のコマンドを使用します。

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

コンテキストは、Androidで実装し、マニフェストで宣言する必要があります。 Androidサービスを使用する方法の完全なチュートリアルについては、Android Services tutorialをご覧ください。

関連する問題