2015-06-09 78 views
0

我試圖通過在intentservice類中實現它來使用Fused位置提供程序API來獲取位置,同時我正在同一服務中啓動一個newtimertask。Android:意圖服務

因此,當intentservice被調用時,timertask開始並且googleapiclent被連接到獲取位置。

我希望如果位置不可用,我的時間任務將在60秒內斷開googleapiclient。

但是這不起作用...如果它沒有得到位置...意圖服務繼續運行或fusedlocatinprovider保持尋找location.so我必須停止這些?

package com.example.rj.intentlocationapi; 

import android.app.AlarmManager; 
import android.app.IntentService; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.CountDownTimer; 
import android.os.SystemClock; 
import android.util.Log; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
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.location.LocationServices; 


public class BackgrndService extends IntentService implements  LocationListener, 
    GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener { 

LocationRequest mLocationRequest; 
GoogleApiClient mGoogleApiClient; 
Location mCurrentLocation; 
LocationManager mlocationmanger; 
int bool = 0; 
CountDownTimer cnt; 


public BackgrndService() { 
    super("BackgrndService"); 
} 


protected void createLocationRequest() { 
    mLocationRequest = new LocationRequest(); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
} 

private boolean isGooglePlayServicesAvailable() { 
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
    if (ConnectionResult.SUCCESS == status) { 
     return true; 
    } else { 
     return false; 
    } 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    if (!isGooglePlayServicesAvailable()) { 
     stopLocationService(); 
    } 
    mlocationmanger = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 


    createLocationRequest(); 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addApi(LocationServices.API) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .build(); 
    cnt = new CountDownTimer(60000, 1000) { 

     public void onTick(long millisUntilFinished) { 

     } 

     public void onFinish() { 
      Log.w("haha", "going to finish it"); 
      stopLocationService(); 


     } 
    }.start(); 
    mGoogleApiClient.registerConnectionCallbacks(this); 
    mGoogleApiClient.connect(); 
    Log.w("haha", "blocked to finish it"); 


} 

public void onConnected(Bundle bundle) { 
    Log.w("hello", "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected()); 
    startLocationUpdates(); 
} 

protected void startLocationUpdates() { 
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
} 

@Override 
public void onConnectionSuspended(int i) { 
    Log.w("hhaahaa", "Connection suspended stop here "); 


} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 
    Log.w("hhaahaa", "Connection failed stop here "); 
} 

@Override 
public void onLocationChanged(Location location) { 
    Log.w("hello", "Firing onLocationChanged.............................................."); 
    mCurrentLocation = location; 
    if (mCurrentLocation != null) { 
     Log.w("hi", mCurrentLocation.getLatitude() + " " + mCurrentLocation.getLongitude()); 

    } 
    stopLocationService(); 
} 


public void stopLocationService() { 

    Log.w("haha", "killing begins"); 

    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
    mGoogleApiClient.disconnect(); 
    cnt.cancel(); 

    Intent alarm = new Intent(this, BackgrndService.class); 


    PendingIntent pendingIntent = PendingIntent.getService(this, 0, alarm, 0); 

    if (bool == 0) { 
     AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
     alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() 
       + 1000 * 100, pendingIntent); 
     bool = 1; 
    } 

} }時onHandleIntent()結束,如果沒有更多的命令已被髮送給它而onHandleIntent

+0

郵編....... – Bharatesh

回答

0

您可以將Intent-Service轉移或更改爲Service,並使用stopSelf()方法停止服務。此方法會停止服務。

+0

我不必使用服務 – Rumour

+0

@Rumour不,你**有**使用服務 – pskink

+0

但那會阻止我的用戶界面...因爲服務在UI線程上運行 – Rumour

0

IntentService自動停止本身()是在運行。因此,您不要自己手動停止IntentService。

+0

您可以將Intent-Service轉移或更改爲Service,並使用stopSelf()方法停止服務。 –

+0

但是,這會使我的用戶界面無響應 – Rumour

+1

進一步我必須基本註銷googelapiclient ........... – Rumour