2016-11-06 55 views
0

我做了一些服务,一些观察者需要订阅获取一些数据。问题在于服务不在构造函数中,所以实例从未创建!如何强制这个或有另一种方式来做到这一点?我正在使用IONIC2框架的事件。即使我不使用它,我也可以在构造函数中添加类,但也许有最佳解决方案。提前致谢。如何强制angular2创建提供者的实例?

import {Injectable } from "@angular/core"; 
import {log, PRIORITY_INFO, PRIORITY_ERROR} from "./log"; 
import { BackgroundGeolocation } from 'ionic-native'; 
import Timer = NodeJS.Timer; 
import {Platform, Events} from "ionic-angular"; 

@Injectable() 
export class BackgroundGeolocationService { 

    trackerInterval: Timer; 
    locations: any; 

    constructor(private platform: Platform, public trace: log, private events: Events) { 
     this.trace.info('create BackgroundGeolocationService'); 

     if (this.platform.is('android')) { 
      this.platform.ready().then(() => { 

       this.trace.info(`platform android ready`); 

       // BackgroundGeolocation is highly configurable. See platform specific configuration options 
       let config = { 
        interval: 1000, 
        locationTimeout: 100, 
        desiredAccuracy: 10, 
        stationaryRadius: 20, 
        distanceFilter: 30, 
        debug: true, // enable this hear sounds for background-geolocation life-cycle. 
        stopOnTerminate: false // enable this to clear background location settings when the app terminates 
       }; 

       BackgroundGeolocation.configure((location) => { 
        this.trace.info(`configure ${location.latitude},${location.longitude}`); 

        this.setCurrentLocation(location); 
        this.startTrackingInterval(); 

        // IMPORTANT: You must execute the finish method here to inform the native plugin that you're finished, 
        // and the background-task may be completed. You must do this regardless if your HTTP request is successful or not. 
        // IF YOU DON'T, ios will CRASH YOUR APP for spending too much time in the background. 
        //BackgroundGeolocation.finish(); // FOR IOS ONLY 

       }, (error) => { 
        this.trace.error(error); 
       }, config); 

       // Turn ON the background-geolocation system. The user will be tracked whenever they suspend the app. 
       BackgroundGeolocation.start(); 

      }).catch(err => { 
       this.trace.error(err); 
      }); 

     } 
    } 

    startTrackingInterval(): void { 
     this.trackerInterval = setInterval(() => { this.refreshLocations(); }, 2000); 
    } 

    refreshLocations(): void { 
     BackgroundGeolocation.getLocations().then(locations => { 
      this.locations = locations; 
      if (locations.length != 0) { 
       this.setCurrentLocation(locations[locations.length-1]); 
      } 
     }).catch(error => { 
      this.trace.error(`BackgroundGeolocationService error ${error}`); 
     }); 
    } 

    startTracker(): void { 
     BackgroundGeolocation.deleteAllLocations(); 
     BackgroundGeolocation.start(); 
    } 

    setCurrentLocation(location: {latitude:string, longitude:string}) { 
     this.events.publish('BackgroundGeolocationService:setCurrentLocation', location); 
    } 

    stopTracking(): void { 
     clearInterval(this.trackerInterval); 
     BackgroundGeolocation.getLocations().then(locations => { 
      this.locations = locations; 
      if (locations.length != 0) { 
       this.setCurrentLocation(locations[locations.length-1]); 
      } 
     }).catch(error => { 
      this.trace.error(error); 
     }); 
     BackgroundGeolocation.stop(); 
    } 
} 
+0

请张贴一些代码,所以我们可以得到你的问题的一个更好的主意强制执行。 – Brad

+0

你可以看看https://github.com/prolland006/ionic2_test_plugin/blob/master/src/services/background-geolocation-service.ts –

回答

0

您可以通过添加一个APP_INITIALIZER提供商依赖于它

{ provide: APP_INITIALIZER, 
     useFactory: (config: BackgroundGeolocationService) =>() => Promise.resolve(null), 
     deps: [BackgroundGeolocationService], 
     multi: true } 
+0

非常interresting谢谢:)。但!我想我会使用Observers,它可以更好地管理错误。有了IONIC2的事件,如果有错误,观察者什么都不会收到,也不知道为什么。 –

+0

不知道你的意思。我根本不知道离子。所有这些都是在应用程序启动时创建一个'BackgroundGeolocationService'的新实例。 –