2016-07-21 13 views
0

LocationManagerを使用してユーザーの現在地を取得しようとしていますが、onLocationChangedが呼び出されることはありません。onLocationChangedは決して呼び出されません - Android

はここにここに私のコード

public void checkLocationService(){ 
    LocationManager lm = (LocationManager)MapsActivity.this.getSystemService(Context.LOCATION_SERVICE); 
    boolean gps_enabled = false; 
    boolean network_enabled = false; 

    try { 
     gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    } catch(Exception ex) { 
     ex.printStackTrace(); 
    } 

    try { 
     network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
    } catch(Exception ex) { 
     ex.printStackTrace(); 
    } 

    if(!gps_enabled && !network_enabled) { 
     // notify user 
     AlertDialog.Builder dialog = new AlertDialog.Builder(MapsActivity.this); 
     dialog.setTitle("Perhatian"); 
     dialog.setMessage("Anda harus mengaktifkan GPS pada perangkat anda untuk dapat menggunakan semua fitur yang ada. Aktifkan GPS?"); 
     dialog.setPositiveButton("Pengaturan", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface paramDialogInterface, int paramInt) { 
       // TODO Auto-generated method stub 
       Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       MapsActivity.this.startActivity(myIntent); 
      } 
     }); 
     dialog.setNegativeButton("Batal", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface paramDialogInterface, int paramInt) { 
       // TODO Auto-generated method stub 

      } 
     }); 
     dialog.show(); 
    } 

    if (gps_enabled) { 
     try { 
      lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
     } catch (SecurityException e) { 

     } 

     if (lm != null) { 
      try { 
       lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
      } catch (SecurityException e) { 

      } 
     } 
    } 
    if (network_enabled) { 
     try { 
      lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 
     } catch (SecurityException e) { 

     } 

     if (lm != null) { 
      try { 
       lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
      } catch (SecurityException e) { 

      } 
     } 
    } 

} 

だ、ここで何が問題になっていますonLocationChanged

@Override 
public void onLocationChanged(Location l) { 
    pointLatitude = l.getLatitude(); 
    pointLongitude = l.getLongitude(); 

    //Fetching username from shared preferences 
    SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE); 
    final String username = sharedPreferences.getString(Config.USERNAME_SHARED_PREF, "Tidak tersedia"); 

    final ProgressDialog loading = ProgressDialog.show(this, "Login...", "Mohon tunggu...", true, false); 
    //Creating a string request 
    StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.GET_LOCATION_URL, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        loading.dismiss(); 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        //You can handle error here if you want 
        //Toast.makeText(LoginActivity.this,error.toString(),Toast.LENGTH_LONG).show(); 
        loading.dismiss(); 
       } 
      }){ 
     @Override 
     protected Map<String, String> getParams() throws AuthFailureError { 
      Map<String,String> params = new HashMap<>(); 
      //Adding parameters to request 
      params.put(Config.KEY_USERNAME, username); 
      params.put(Config.KEY_LATITUDE, String.valueOf(pointLatitude)); 
      params.put(Config.KEY_LONGITUDE, String.valueOf(pointLongitude)); 

      //returning parameter 
      return params; 
     } 
    }; 

    //Adding the string request to the queue 
    RequestQueue requestQueue = Volley.newRequestQueue(this); 
    requestQueue.add(stringRequest); 
} 

ですか?前もって感謝します。

+0

あなたのコードはどこに登録されていますか?サービスがサポートされているかどうかをチェックしていますが、提供したスニペットのアップデートを購読していません。 – ScouseChris

+0

'onCreate'に' checkLocationService() 'を入れました ロケーションの更新を登録するにはどうしたらいいですか? –

+0

ここにMainActivityコードを投稿してください。 –

答えて

0
first check below permission is required. 


<uses-permission android:name="android.permission.ACCESS_GPS" /> 
<uses-permission android:name="android.permission.ACCESS_LOCATION" /> 
<uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.INTERNET" /> 


Then please check that your class need to extend LocationListener 

public class MyLocationListener implements LocationListener { 
@Override 
    public void onLocationChanged(Location arg0) { 
    latitude = arg0.getLatitude(); 
    longitude = arg0.getLongitude(); 
    } 

} 
+1

私は自分のコードで使用しましたが、まだ動作しません:( –

+0

onLocationChangedを横に入れて緯度と経度を設定してくださいToast.make(コンテキスト、 "latitude:" + latitude、Toast。*)。show() – ViratBhavsar

+0

トーストが呼び出されていません:( –

関連する問題