0
我是Xamarin開發新手。我創建了一個用於跟蹤用戶位置的後臺服務。它正常工作,直到應用程序運行/打開時爲止。隨着應用程序被關閉/銷燬,我的後臺服務停止工作。我已經花了很多時間來找出這個問題背後的原因。但運氣不好。在Xamarin Android應用程序關閉後,後臺服務會停止
[Service]
public class LocationTrackingService : Service, GoogleApiClient.IConnectionCallbacks,
GoogleApiClient.IOnConnectionFailedListener, Android.Gms.Location.ILocationListener
{
protected const string TAG = "Attendance Frag";
protected const int REQUEST_CHECK_SETTINGS = 0x1;
public const long UPDATE_INTERVAL_IN_MILLISECONDS = 20 * 1000;
public const long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS/2;
protected const string KEY_REQUESTING_LOCATION_UPDATES = "requesting-location-updates";
protected const string KEY_LOCATION = "location";
protected const string KEY_LAST_UPDATED_TIME_STRING = "last-updated-time-string";
protected GoogleApiClient mGoogleApiClient;
protected LocationRequest mLocationRequest;
protected LocationSettingsRequest mLocationSettingsRequest;
public override void OnCreate()
{
base.OnCreate();
BuildGoogleApiClient();
CreateLocationRequest();
Toast.MakeText(this, "OnCreate", ToastLength.Short).Show();
}
protected void BuildGoogleApiClient()
{
Log.Info("AttendanceFrag", "Building GoogleApiClient");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.AddConnectionCallbacks(this)
.AddOnConnectionFailedListener(this)
.AddApi(LocationServices.API)
.Build();
}
protected void CreateLocationRequest()
{
mLocationRequest = new LocationRequest();
mLocationRequest.SetInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.SetFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.SetPriority(LocationRequest.PriorityHighAccuracy);
}
protected void BuildLocationSettingsRequest()
{
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.AddLocationRequest(mLocationRequest);
builder.SetAlwaysShow(true);
mLocationSettingsRequest = builder.Build();
}
public override IBinder OnBind(Intent intent)
{
return null;
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
base.OnStartCommand(intent, flags, startId);
if (mGoogleApiClient != null && !mGoogleApiClient.IsConnected)
mGoogleApiClient.Connect();
return StartCommandResult.Sticky;
}
protected async Task StartLocationUpdates()
{
await LocationServices.FusedLocationApi.RequestLocationUpdates(
mGoogleApiClient,
mLocationRequest,
this
);
}
protected async Task StopLocationUpdates()
{
await LocationServices.FusedLocationApi.RemoveLocationUpdates(
mGoogleApiClient,
this
);
}
public async void OnConnected(Bundle connectionHint)
{
Log.Info(TAG, "Connected to GoogleApiClient");
await StartLocationUpdates();
}
public void OnConnectionSuspended(int cause)
{
Log.Info(TAG, "Connection suspended");
}
public void OnConnectionFailed(Android.Gms.Common.ConnectionResult result)
{
Log.Info(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.ErrorCode);
}
public void OnLocationChanged(Location location)
{
Toast.MakeText(this, "OnLocationChanged", ToastLength.Short).Show();
}
public override async void OnDestroy()
{
base.OnDestroy();
Toast.MakeText(this, "OnDestroy", ToastLength.Short).Show();
if (mGoogleApiClient != null
&& mGoogleApiClient.IsConnected)
{
await StopLocationUpdates();
}
//Start GeoLocation Tracking Service again
Intent i = new Intent(this, typeof(RestartServiceReceiver));
i.SetAction(RestartServiceReceiver.START_TRACKING);
SendBroadcast(i);
}
}
請幫我
你是否已經在'manifest.xml'文件中註冊了服務? – Fahadsk
正如我所知,通過定義[Service],服務會自動註冊到manifest.xml中,並注意服務工作正常,直到應用程序打開爲止。關閉應用程序後,服務停止工作。 –