2017-06-01 189 views
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); 

    } 
} 

请帮我

+0

你是否已经在'manifest.xml'文件中注册了服务? – Fahadsk

+0

正如我所知,通过定义[Service],服务会自动注册到manifest.xml中,并注意服务工作正常,直到应用程序打开为止。关闭应用程序后,服务停止工作。 –

回答

0

在一个私人的过程中开始你的服务就像在过程here运行服务这

[Service(Name = "com.xamarin.TimestampService", 
     Process=":timestampservice_process", 
     Exported=true)] 

更多信息。 你也可以注册为foreground service这样它们不会被android停下来,并且会在通知栏中显示

+0

无法正常工作。可以通过发布任何满足我要求的工作/测试演示代码来支持您。 –

相关问题