首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我需要帮助实现离子前景服务

我需要帮助实现离子前景服务
EN

Stack Overflow用户
提问于 2020-01-21 04:02:59
回答 1查看 1.6K关注 0票数 2

我有问题,让我的应用程序在后台运行,其中之一是应用程序跟踪位置时,设备是在后台模式。一切正常,但后台服务只工作2分钟。我正在pie - Android 9上进行测试。我从android文档中看到,我需要为android 8及更高版本使用前台服务。我想知道前台服务在我的离子4.6应用程序中的实现

我的app.component.ts文件代码。

代码语言:javascript
复制
import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { Platform } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { AngularFireAuth } from '@angular/fire/auth';
import { ForegroundService } from '@ionic-native/foreground-service/ngx';



declare var cordova: any;

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html'
})
export class AppComponent {
  constructor(
    private router: Router,
    private platform: Platform,
    private splashScreen: SplashScreen,
    private statusBar: StatusBar,
    private fireAuth: AngularFireAuth,
    public foregroundService: ForegroundService
    ) {
    this.initializeApp();
  }

  initializeApp() {
    this.platform.ready().then(() => {
      cordova.plugins.backgroundMode.on('activate', () => {
        cordova.plugins.backgroundMode.disableWebViewOptimizations();
        cordova.plugins.backgroundMode.disableBatteryOptimizations();
        console.log('ACTIVATE background mode1');
        cordova.plugins.backgroundMode.setEnabled(true);
            });
      this.fireAuth.auth.onAuthStateChanged(user => {
        if (user) {
          this.router.navigate(['/tabs/tab2']);
        } else {
          this.router.navigate(['/slider']);
          this.splashScreen.hide();
        }
      });
      this.statusBar.styleDefault();
      this.foregroundService.start('GPS Running', 'Background Service', 'drawable/fsicon');
    });
  }
}
EN

回答 1

Stack Overflow用户

发布于 2020-12-03 20:12:19

BackgroundMode和ForegroundService添加到app.module.ts中的提供程序列表中

代码语言:javascript
复制
import { ForegroundService } from '@ionic-native/foreground-service/ngx';
import { BackgroundMode } from '@ionic-native/background-mode/ngx';

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';

import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';

@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  imports: [
    BrowserModule,
    IonicModule.forRoot(),
    AppRoutingModule,
  ],
  providers: [
    StatusBar,
    SplashScreen,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
    BackgroundMode,
    ForegroundService,
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

此外,还需要将以下代码添加到config.xml文件中

代码语言:javascript
复制
<platform name="android">
    .
    .
    .
    <config-file parent="/*" target="AndroidManifest.xml">
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    </config-file>
</platform>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59834016

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档