0

나는 오프라인 자동차 추적기 안드로이드 응용 프로그램을 개발 중입니다. 5 분 후에 위치를 업데이트하고 SQLite에 저장합니다. FusedLocationAPI을 사용했지만 인터넷없이 버스를 타고 여행 할 때 정확한 위치를 얻을 수 없습니다. 나는 999m의 정확도를 얻고 있으며 매 5 분마다 동일한 위치를 얻는다. 알람 관리자를 5 분으로 설정했습니다.인터넷없이 버스에서 여행하는 동안 정확한 위치를 얻을 수 없습니다

public static void startAlarmManager(Context context) 
{ 
    preferences =context.getSharedPreferences(Constant.SHARED_PREF_NAME, Context.MODE_PRIVATE); 
    int duration= Integer.parseInt(preferences.getString(Constant.DURATION_SHARED_PREF,Constant.CONSTANT_DURATION_SHARED_PREF)); 
    Log.d("duration",duration+""); 



    alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
    gpsTrackerIntent = new Intent(context, GpsTrackerAlarmReceiver.class); 
    pendingIntent = PendingIntent.getBroadcast(context, 0, gpsTrackerIntent, 0); 

    alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
      SystemClock.elapsedRealtime(), 
      5*60000, 
      pendingIntent); 
} 

브로드 캐스트 수신기가 작동하지 않습니다.

public class GpsTrackerAlarmReceiver extends WakefulBroadcastReceiver { 

private static final String TAG = "GpsTrackerAlarmReceiver"; 


@Override 
public void onReceive(Context context, Intent intent) { 


    LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
    boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER); 

    if(statusOfGPS) { 

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

    } 
} 



} 

이것은 위치 서비스입니다. 나는이 방법으로 위치를 얻고있다.

public class LocationService extends Service implements 
    GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, 
    LocationListener { 
private static final String TAG = "LocationService"; 
public static GoogleApiClient googleApiClient; 
private LocationRequest locationRequest; 
public Context context; 

private DatabaseHelper db; 

private boolean currentlyProcessingLocation = false; 



@Override 
public void onCreate() { 
    super.onCreate(); 

} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Log.d(TAG, "onStartCommand"); 

    if (!currentlyProcessingLocation) { 
     currentlyProcessingLocation = true; 
     startTracking(); 
    } 


    return START_NOT_STICKY; 
} 

private void startTracking() { 


    if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS) { 

     googleApiClient = new GoogleApiClient.Builder(this) 
       .addApi(LocationServices.API) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .build(); 

     if (!googleApiClient.isConnected() || !googleApiClient.isConnecting()) { 
      googleApiClient.connect(); 
     } 
    } else { 
     Log.e(TAG, "unable to connect to google play services."); 
    } 
} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 

    Log.d(TAG, "onConnected"); 



    locationRequest = LocationRequest.create(); 
    locationRequest.setInterval(1000); // milliseconds 
    locationRequest.setFastestInterval(1000); // the fastest rate in milliseconds at which your app can handle location updates 
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 


    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

     return; 
    } 
    LocationServices.FusedLocationApi.requestLocationUpdates(
      googleApiClient, locationRequest, this); 

} 

@Override 
public void onConnectionSuspended(int i) { 
    Log.e(TAG, "GoogleApiClient connection has been suspend"); 
} 

@Override 
public void onLocationChanged(Location location) { 
    Log.d(TAG, "onLocationChanged"); 

    startupdate(location); 

} 

private void startupdate(Location location) { 

    if (location != null) { 

     db=new DatabaseHelper(this); 

     db.insertLocation(location.getLatitude(), location.getLongitude(), "FusedApi Provider", location.getAccuracy()); 


     stopLocationUpdates(); 
     stopSelf(); 

    } 
} 

public void stopLocationUpdates() { 
    if (googleApiClient != null && googleApiClient.isConnected()) { 
     googleApiClient.disconnect(); 
    } 
} 


@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

    Log.e(TAG, "onConnectionFailed"); 

    stopLocationUpdates(); 
    stopSelf(); 
} 

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



} 

제발 ... 제 잘못입니다. 미리 감사드립니다.

+0

gps를 사용하십시오. 창문에 앉아. 또는 창을 통해 휴대 전화를 외부에 두십시오. – greenapps

+0

답장을 보내 주셔서 감사합니다. 오프라인 모드에서 위치 정보를 가져 오는 것이 어렵습니까? – swap9

답변

0

GPS가 실용적이지 않은 경우 LocationManager.NETWORK_PROVIDER를 사용할 수 있습니다. 이는 GPS보다 정확하지는 않지만 이동 통신사에 타워가있는 전화 통신 업체의 위치입니다. 내가하는 일은 플래그 isGpsAvailable()을 설정하는 것입니다. 이것이 사실이라면 GPS를 사용합니다. 그렇지 않으면 네트워크 제공 위치를 사용합니다. This Google's doc은 사용할 수있는 코드 스 니펫을 포함하여 자세한 솔루션을 제공하며 사용자의 요구에 맞는 방식으로 변경합니다.