2016-10-27 1 views
0

앱이 시작될 때와 새로 고침 버튼이 눌려지면 GPS를 모두 확인하고 싶습니다. 날씨 정보를 호출하기 위해 mLatitude 및 mLongitude의 형태로 데이터 포인트를 표시합니다. 결국 나는 도시를 지오 코딩 할 예정이지만, 디버깅 목적으로 GPS 좌표를 locationLabel 텍스트 뷰로 출력하고 있습니다.내 안드로이드 앱에 GPS 기능을 추가하려고 할 때마다 GPS 좌표가 0.0, 0.0으로 바뀝니다.

내 MainActivity.java :

package com.example.paxie.stormy.ui; 

import android.Manifest; 
import android.annotation.TargetApi; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.content.pm.PackageManager; 
import android.graphics.drawable.Drawable; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.os.Build; 
import android.os.Bundle; 
import android.provider.Settings; 
import android.support.annotation.NonNull; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.content.ContextCompat; 
import android.support.v7.app.ActionBarActivity; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.ProgressBar; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.example.paxie.stormy.GPS_Service; 
import com.example.paxie.stormy.R; 
import com.example.paxie.stormy.weather.Current; 
import com.example.paxie.stormy.weather.Day; 
import com.example.paxie.stormy.weather.Forecast; 
import com.example.paxie.stormy.weather.Hour; 
import com.squareup.okhttp.Call; 
import com.squareup.okhttp.Callback; 
import com.squareup.okhttp.OkHttpClient; 
import com.squareup.okhttp.Request; 
import com.squareup.okhttp.Response; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.IOException; 

import butterknife.Bind; 
import butterknife.ButterKnife; 
import butterknife.OnClick; 


public class MainActivity extends AppCompatActivity { 

    public static final String TAG = MainActivity.class.getSimpleName(); 
    public static final String DAILY_FORECAST = "DAILY_FORECAST"; 
    public static final String HOURLY_FORECAST = "HOURLY_FORECAST"; 
    private Forecast mForecast; 
    private double mLatitude; 
    private double mLongitude; 
    private BroadcastReceiver broadcastReceiver; 

    @Bind(R.id.timeLabel) 
    TextView mTimeLabel; 
    @Bind(R.id.temperatureLabel) 
    TextView mTemperatureLabel; 
    @Bind(R.id.humidityValue) 
    TextView mHumidityValue; 
    @Bind(R.id.precipValue) 
    TextView mPrecipValue; 
    @Bind(R.id.summaryLabel) 
    TextView mSummaryLabel; 
    @Bind(R.id.iconImageView) 
    ImageView mIconImageView; 
    @Bind(R.id.refreshImageView) 
    ImageView mRefreshImageView; 
    @Bind(R.id.progressBar) 
    ProgressBar mProgressBar; 
    @Bind(R.id.locationLabel) 
    TextView mLocationlabel; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ButterKnife.bind(this); 

     mProgressBar.setVisibility(View.INVISIBLE); 

     mRefreshImageView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       getForecast(); 
      } 
     }); 


     getForecast(); 
     Log.d(TAG, "Main UI code is running!"); 
    } 

    private void getForecast() { 
     if(!runtime_permissions()) 
     checkGPS(); 
     String apiKey = "1621390f8c36997cb1904914b726df52"; 
     String forecastUrl = "https://api.forecast.io/forecast/" + apiKey + 
       "/" + mLatitude + "," + mLongitude; 

     if (isNetworkAvailable()) { 
      toggleRefresh(); 

      OkHttpClient client = new OkHttpClient(); 
      Request request = new Request.Builder() 
        .url(forecastUrl) 
        .build(); 

      Call call = client.newCall(request); 
      call.enqueue(new Callback() { 
       @Override 
       public void onFailure(Request request, IOException e) { 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          toggleRefresh(); 
         } 
        }); 
        alertUserAboutError(); 
       } 

       @Override 
       public void onResponse(Response response) throws IOException { 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          toggleRefresh(); 
         } 
        }); 
        try { 
         String jsonData = response.body().string(); 
         Log.v(TAG, jsonData); 
         if (response.isSuccessful()) { 
          mForecast = parseForecastDetails(jsonData); 
          runOnUiThread(new Runnable() { 
           @Override 
           public void run() { 
            updateDisplay(); 
           } 
          }); 

         } else { 
          alertUserAboutError(); 

         } 
        } catch (IOException e) 

        { 
         Log.e(TAG, "Exception caught: ", e); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }); 
     } else { 
      Toast.makeText(this, "Network is currently unavailable!", Toast.LENGTH_LONG).show(); 
     } 
    } 


    private void toggleRefresh() { 
     if (mProgressBar.getVisibility() == View.INVISIBLE) { 
      mProgressBar.setVisibility(View.VISIBLE); 
      mRefreshImageView.setVisibility(View.INVISIBLE); 
     } else { 
      mProgressBar.setVisibility(View.INVISIBLE); 
      mRefreshImageView.setVisibility(View.VISIBLE); 
     } 
    } 

    private void updateDisplay() { 
     mLocationlabel.setText(mLatitude + " " + mLongitude); 
     Current current = mForecast.getCurrent(); 
     mTemperatureLabel.setText(current.getTemperature() + ""); 
     mTimeLabel.setText("At " + current.getFormattedTime() + " it will be:"); 
     mHumidityValue.setText(current.getHumidity() + ""); 
     mPrecipValue.setText(current.getPrecipChance() + "%"); 
     mSummaryLabel.setText(current.getSummary()); 
     Drawable drawable = ContextCompat.getDrawable(this, current.getIconId()); 
     mIconImageView.setImageDrawable(drawable); 
    } 

    private Forecast parseForecastDetails(String jsonData) throws JSONException { 
     Forecast forecast = new Forecast(); 
     forecast.setCurrent(getCurrentDetails(jsonData)); 
     forecast.setHourlyForecast(getHourlyForecast(jsonData)); 
     forecast.setDailyForecast(getDailyForecast(jsonData)); 

     return forecast; 

    } 

    private Day[] getDailyForecast(String jsonData) throws JSONException { 
     JSONObject forecast = new JSONObject(jsonData); 
     String timezone = forecast.getString("timezone"); 
     JSONObject daily = forecast.getJSONObject("daily"); 
     JSONArray data = daily.getJSONArray("data"); 

     Day[] days = new Day[data.length()]; 

     for (int i = 0; i < data.length(); i++) { 
      JSONObject jsonDay = data.getJSONObject(i); 
      Day day = new Day(); 

      day.setSummary(jsonDay.getString("summary")); 
      day.setIcon(jsonDay.getString("icon")); 
      day.setTemperatureMax(jsonDay.getDouble("temperatureMax")); 
      day.setTime(jsonDay.getLong("time")); 
      day.setTimeZone(timezone); 

      days[i] = day; 

     } 
     return days; 
    } 

    private Hour[] getHourlyForecast(String jsonData) throws JSONException { 
     JSONObject forecast = new JSONObject(jsonData); 
     String timezone = forecast.getString("timezone"); 
     JSONObject hourly = forecast.getJSONObject("hourly"); 
     JSONArray data = hourly.getJSONArray("data"); 

     Hour[] hours = new Hour[data.length()]; 

     for (int i = 0; i < data.length(); i++) { 
      JSONObject jsonHour = data.getJSONObject(i); 
      Hour hour = new Hour(); 
      hour.setSummary(jsonHour.getString("summary")); 
      hour.setTemperature(jsonHour.getDouble("temperature")); 
      hour.setIcon(jsonHour.getString("icon")); 
      hour.setTime(jsonHour.getLong("time")); 
      hour.setTimeZone(timezone); 

      hours[i] = hour; 

     } 
     return hours; 
    } 

    private Current getCurrentDetails(String jsonData) throws JSONException { 
     JSONObject forecast = new JSONObject(jsonData); 
     String timezone = forecast.getString("timezone"); 
     Log.i(TAG, "From JSON: " + timezone); 

     JSONObject currently = forecast.getJSONObject("currently"); 
     Current current = new Current(); 
     current.setHumidity(currently.getDouble("humidity")); 
     current.setTime(currently.getLong("time")); 
     current.setIcon(currently.getString("icon")); 
     current.setPrecipChance(currently.getDouble("precipProbability")); 
     current.setSummary(currently.getString("summary")); 
     current.setTemperature(currently.getDouble("temperature")); 
     current.setTimeZone(timezone); 

     Log.d(TAG, current.getFormattedTime()); 

     return current; 
    } 

    private boolean isNetworkAvailable() { 
     ConnectivityManager manager = (ConnectivityManager) 
       getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo networkInfo = manager.getActiveNetworkInfo(); 
     boolean isAvailable = false; 
     if (networkInfo != null && networkInfo.isConnected()) { 
      isAvailable = true; 
     } 
     return isAvailable; 
    } 


    private void alertUserAboutError() { 
     AlertDialogFragment dialog = new AlertDialogFragment(); 
     dialog.show(getFragmentManager(), "error_dialog"); 
    } 


    @OnClick(R.id.dailyButton) 
    public void startDailyActivity(View view) { 
     Intent intent = new Intent(this, DailyForecastActivity.class); 
     intent.putExtra(DAILY_FORECAST, mForecast.getDailyForecast()); 
     startActivity(intent); 

    } 

    @OnClick(R.id.hourlyButton) 
    public void startHourlyActivity(View view) { 
     Intent intent = new Intent(this, HourlyForecastActivity.class); 
     intent.putExtra(HOURLY_FORECAST, mForecast.getHourlyForecast()); 
     startActivity(intent); 
    } 

    private void checkGPS() { 
     if (broadcastReceiver == null) { 
      broadcastReceiver = new BroadcastReceiver() { 
       @Override 
       public void onReceive(Context context, Intent intent) { 
        mLatitude = (double) intent.getExtras().get("latitude"); 
        mLongitude = (double) intent.getExtras().get("longitude"); 
       } 
      }; 
     } 
     registerReceiver(broadcastReceiver, new IntentFilter("location_update")); 
    } 

    private boolean runtime_permissions() { 
     if (Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

      requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 100); 

      return true; 
     } 
     return false; 
    } 


    @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
     if (requestCode == 100) { 
      if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) { 
       getForecast(); 
      } else { 
       runtime_permissions(); 
      } 
     } 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     checkGPS(); 
    } 
} 

내 GPSservice.java :

1. 당신이 포함했는지 확인하십시오 : 당신이 시도 할 수

package com.example.paxie.stormy; 

import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.provider.Settings; 
import android.support.annotation.Nullable; 

/** 
* Created by paxie on 10/27/16. 
*/ 
public class GPS_Service extends Service { 
    private LocationListener listener; 
    private LocationManager locationManager; 

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

    @Override 
    public void onCreate() { 

     listener = new LocationListener() { 
      @Override 
      public void onLocationChanged(Location location) { 
       Intent i = new Intent("location_update"); 
       i.putExtra("coordinates",location.getLongitude()+" "+location.getLatitude()); 
       i.putExtra("longitude", location.getLongitude()); 
       i.putExtra("latitude", location.getLatitude()); 
       sendBroadcast(i); 
      } 

      @Override 
      public void onStatusChanged(String s, int i, Bundle bundle) { 

      } 

      @Override 
      public void onProviderEnabled(String s) { 

      } 

      @Override 
      public void onProviderDisabled(String s) { 
       Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       startActivity(i); 
      } 
     }; 

     locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE); 

     //noinspection MissingPermission 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,0,listener); 

    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     if(locationManager != null){ 
      //noinspection MissingPermission 
      locationManager.removeUpdates(listener); 
     } 
    } 
} 
+0

내 대답을 보았습니까? 이걸로 어디까지 도달 했습니까? –

답변

0

은 다음 두 가지입니다 매니페스트의 사용 권한은 ACCESS_FINE_LOCATIONACCESS_COARSE_LOCATION입니다. 앱이 Android API 23 이상을 타겟팅하는 경우 런타임 권한 처리를 포함해야합니다. 대신 클래스의 구성원으로 LocationListener 객체를 필요없이

2., 당신은 당신이 Service 인터페이스로 구현하고 onLocationChanged() 메소드를 구현해야한다.

외에도 AOSP에있는 위치 API (예 : android.location)보다는 Google Play 서비스에서 위치 API를 사용하는 것이 좋습니다.

Making Your App Location-Aware