2017-06-01 32 views
0

私はXamarin開発が初めてです。私は、ユーザーの場所を追跡するために使用される1つのバックグラウンドサービスを作成しました。アプリケーションが実行中/実行中になるまでは正常に動作しています。アプリケーションが終了/破棄されると、私のバックグラウンドサービスは機能しなくなります。私はすでにこの問題の背後にある理由を知るために多くの時間を費やしました。しかし、運が悪い。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

私は[サービス]を定義して知っているように、サービスは自動的にmanifest.xmlに登録され、アプリケーションが開いている間は正常に動作していることに注意してください。 –

答えて

0
は、プロセス hereでサービスを実行している上、この

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

の詳細情報などのプライベートプロセスであなたのサービスを開始し

してください。 あなたはまた、foreground serviceとして登録することができます。アンドロイドによって止まらず、通知バーに表示されます。

+0

もう動作しません。私の要求を満たすデモコードを投稿してください。 –

関連する問題