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()を設定して、それが真であるかどうかを確認します。それ以外の場合、私はネットワークで提供された場所を使用します。 This Google's docは、使用できるコードスニペットを含む詳細なソリューションを提供し、ニーズに合った方法を変更します。

関連する問題