2016-09-05 5 views
0

우선이 사이트의 다른 질문과 중복되지 않습니다. Service과 Google API를 사용하여 위치 업데이트를 확인했습니다. Here is the link which uses the service to get location updates Google 위치 서비스 API를 사용하여 백그라운드에서 위치 업데이트를받는 방법은 무엇인가요?

  • This is the link which uses Google API for the same. This works perfectly.
  • 나는 모두를 시도했지만, 서비스의 경우, 그것은 작동하지 않습니다

    • . 하지만 첫 번째 링크의 코드는 완벽하게 작동하며 위치 업데이트를 올바르게 제공합니다. 그러나 나는 앱이 백그라운드에있을 때 업데이트를 얻을 수 없다고 생각한다.

      내 앱이 백그라운드에서 계속해서 위치 업데이트를받을 수있는 서비스 내에 코드를 넣고 싶습니다.

      그러나이 두 코드를 병합하는 방법을 알 수 없습니다. 내가 뭘 봤는지 물어 보는거야? 서비스 클래스에서 일반적인 메소드를 복사하기 만하면됩니다. . 그러나이 날을 제안 해주십시오 위해 내가 안드로이드에 새로운 해요 가능한 대안이있는 경우 너무 많은 오류 :(

      나에게주는

      감사합니다 사전에

    답변

    0

    보십시오.!

    서비스 된 .java

    import android.app.Service; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.location.Location; 
    import android.os.Bundle; 
    import android.os.IBinder; 
    import android.support.annotation.Nullable; 
    import android.util.Log; 
    import android.widget.Toast; 
    import com.google.android.gms.common.api.GoogleApiClient; 
    import com.google.android.gms.location.LocationListener; 
    import com.google.android.gms.location.LocationRequest; 
    import com.google.android.gms.common.ConnectionResult; 
    import com.google.android.gms.location.LocationServices; 
    
    
    import java.io.IOException; 
    import java.io.OutputStreamWriter; 
    
    public class GPSService extends Service implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,LocationListener { 
    
    
    
    private GoogleApiClient mGoogleApiClient; 
    
    private LocationRequest mLocationRequest; 
    
    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
        return null; 
    } 
    
    
    @Override 
    public void onCreate() { 
        super.onCreate(); 
    
        mGoogleApiClient = new GoogleApiClient.Builder(this) 
          .addApi(LocationServices.API) 
          .addConnectionCallbacks(this) 
          .addOnConnectionFailedListener(this) 
          .build(); 
    
    } 
    
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
    
        mGoogleApiClient.connect(); 
    
        return super.onStartCommand(intent, flags, startId); 
    } 
    
    @Override 
    public void onDestroy() { 
    
    
        mGoogleApiClient.disconnect(); 
        super.onDestroy(); 
    
    
    
    } 
    
    
    @Override 
    public void onConnected(Bundle bundle) { 
        mLocationRequest = LocationRequest.create(); 
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
        mLocationRequest.setInterval(1000); // Update location every second 
    
        LocationServices.FusedLocationApi.requestLocationUpdates(
          mGoogleApiClient, mLocationRequest, this); 
    } 
    
    @Override 
    public void onConnectionSuspended(int i) { 
        Toast.makeText(this,"GoogleApiClient connection has been suspend",Toast.LENGTH_LONG).show(); 
    } 
    
    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
        Toast.makeText(this,"GoogleApiClient connection has failed",Toast.LENGTH_LONG).show(); 
    } 
    

    및 MainActivity.java

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
    
        Intent i = new Intent(this,GPSService.class); 
        startService(i); 
    
    
    } 
    
    관련 문제