2017-09-14 3 views
0

목표 : 나는 이력서를 실행할 때마다 내가의 좌표를 GPS를 사용하여 새 위치를보고 싶다
디스플레이가 다시 시작 단계에서 매번 좌표 GPS

문제 : 사진을 바탕으로
, 그것은 작동하지 않고 나는 또한 "GPS 상태가 Temporarily_unavailable로 변경되었습니다"를 검색합니다. 누락 된 부분이 있습니까?

감사합니다.

정보 : 내가 API 23

enter image description here


안드로이드 manifest.xml을 사용하고 있습니다 * 내가
안드로이드 의 새로운 해요 *

<application android:icon="@mipmap/ic_launcher" android:label="@string/app_name"> 
    <activity android:name=".SimpleGPSTestActivity" 
     android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

</application> 
<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 


SimpleGPSTestActivity.cs

package com.jfdimarzio.test3; 

import android.Manifest; 
import android.app.Activity; 
import android.content.Context; 
import android.content.pm.PackageManager; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.location.LocationProvider; 
import android.os.Bundle; 
import android.support.v4.app.ActivityCompat; 
import android.text.format.Time; 
import android.widget.TextView; 

import java.text.DecimalFormat; 



public class SimpleGPSTestActivity extends Activity { 
    private TextView texten = null; 
    private LocationManager location_manager; 
    private LocationListener location_listener; 

    private void print(String text) { 
     Time now = new Time(); 
     now.setToNow(); 
     String timeString = now.format("%H:%M:%S"); 
     String line = timeString + ": " + text + "\n"; 
     texten.setText(texten.getText() + line); 
     texten.invalidate(); 
     texten.postInvalidate(); 
    } 

    DecimalFormat dec = new DecimalFormat("0.0000"); 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     texten = new TextView(this); 
     texten.setText(""); 
     setContentView(texten); 

     print("onCreate"); 


     /* 
     ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); 


     // Register the listener with the Location Manager to receive 
     // location updates 
     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; 
     } 
*/ 

     location_manager = 
       (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

     location_listener = new LocationListener() { 
      @Override 
      public void onLocationChanged(Location location) { 
       double longitude = location.getLongitude(); 
       double latitude = location.getLatitude(); 
       double accuracy = location.getAccuracy(); 
       double altitude = location.getAltitude(); 
       print("New location: Long " + dec.format(longitude) + 
         ", lat " + dec.format(latitude) + 
         ", accuracy " + dec.format(accuracy) + 
         ", alt " + dec.format(altitude)); 
      } 

      @Override 
      public void onProviderDisabled(String provider) { 
       print("Location provider has been disabled."); 
      } 

      @Override 
      public void onProviderEnabled(String provider) { 
       print("Good news! Location provider has been enabled."); 
      } 

      @Override 
      public void onStatusChanged(String provider, int status, 
             Bundle extras) { 
       if (status == LocationProvider.OUT_OF_SERVICE) 
        print("GPS status changed to OUT_OF_SERVICE."); 
       else if (status == LocationProvider.TEMPORARILY_UNAVAILABLE) 
        print("GPS status changed to TEMPORARILY_UNAVAILABLE."); 
       else if (status == LocationProvider.AVAILABLE) 
        print("Good news! GPS status changed to AVAILABLE."); 
      } 
     }; 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     print("onPause"); 
     location_manager.removeUpdates(location_listener); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     print("onResume"); 
     try { 


      ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); 


      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; 
      } 

      location_manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, location_listener); 
     } 
     catch (Exception e) 
     { 
      print("Couldn't use the GPS: " + e + ", " + e.getMessage()); 
     } 
    } 


    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) 
    { 
     switch (requestCode) { 
      case 1: { 
       // If request is cancelled, the result arrays are empty. 
       if (grantResults.length > 0 
         && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       } else { 
        // permission denied, boo! Disable the 
        // functionality that depends on this permission. 
       } 
       return; 
      } 
      // other 'case' lines to check for other 
      // permissions this app might request 
     } 
    } 





} 

답변

0

당신은 당신에게 반환 할 위치를 얻을 수있는을 locationManager 충분한 시간을 포기하지 않을 것입니다. GPS가 warmed up (거의 항상 있음) 인 하늘을 시야가 잘 보면 위치 업데이트를받는 데 약 5-10 초가 소요됩니다. 다른 상황에서는 지하철이나 GPS가없는 곳에서 시간이 오래 걸릴 수도 있고 잠재적으로 불가능할 수도 있습니다. 이 모든 것의 모습에서 당신은 열어서 즉시 활동을 종료 한 다음 시작하여 업데이트를 중단하는 것처럼 보입니다.

+0

도움 주셔서 감사합니다. –