如何将Foreground服务迁移到ns CLI 6.0?

时间:2019-09-02 11:30:48

标签: android angular typescript nativescript

当我尝试迁移到nativescript 6.0或更高版本时,启动前台服务时遇到问题。这段代码可以在ns 5.1上正常工作...但是在6.0上,我出现如下错误:在路径:DexPathList上找不到类“ com.tns.ForegroundService”

  

日志: 08-28 20:02:45.119 14983 14983 I JS:启动freground 08-28 20:02:45.150 14983 14983 D AndroidRuntime:关闭VM   ---------崩溃开始08-28 20:02:45.154 14983 14983 E AndroidRuntime:致命异常:主要08-28 20:02:45.154 14983 14983 E   AndroidRuntime:流程:org.nativescript.TabNavigation,PID:14983   08-28 20:02:45.154 14983 14983 E Android运行时:   java.lang.RuntimeException:无法实例化服务   com.tns.ForegroundService:java.lang.ClassNotFoundException:没有   在路径:DexPathList [[zip文件]中找到类“ com.tns.ForegroundService”   zip文件“ /system/framework/org.apache.http.legacy.boot.jar”   “ /data/app/org.nativescript.TabNavigation-d2wbgYdHKRNbKAzPZK6rVg==/base.apk"],nativeLibraryDirectories=[/data/app/org.nativescript.TabNavigation-d2wbgYdHKRNbKAzPZK6rVg==/lib   /data/app/org.nativescript.TabNavigation-d2wbgYdHKRNbKAzPZK6rVg==/base.apk!/lib/arm64-v8a,   / system / lib64]] 08-28 20:02:45.154 14983 14983 E AndroidRuntime:
  在   android.app.ActivityThread.handleCreateService(ActivityThread.java:3721)   08-28 20:02:45.154 14983 14983 E AndroidRuntime:at   android.app.ActivityThread.access $ 1400(ActivityThread.java:235)08-28   20:02:45.154 14983 14983 E AndroidRuntime:在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1784)   08-28 20:02:45.154 14983 14983 E AndroidRuntime:at   android.os.Handler.dispatchMessage(Handler.java:106)08-28   20:02:45.154 14983 14983 E AndroidRuntime:在   android.os.Looper.loop(Looper.java:214)08-28 20:02:45.154 14983 14983   E AndroidRuntime:位于   android.app.ActivityThread.main(ActivityThread.java:6986)08-28   20:02:45.154 14983 14983 E AndroidRuntime:在   java.lang.reflect.Method.invoke(本机方法)08-28 20:02:45.154   14983 14983 E AndroidRuntime:在   com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)   08-28 20:02:45.154 14983 14983 E AndroidRuntime:at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)08-28   20:02:45.154 14983 14983 E AndroidRuntime:由以下原因引起:   java.lang.ClassNotFoundException:找不到类   路径上的“ com.tns.ForegroundService”:DexPathList [[zip文件   zip文件“ /system/framework/org.apache.http.legacy.boot.jar”   “ /data/app/org.nativescript.TabNavigation-d2wbgYdHKRNbKAzPZK6rVg==/base.apk"],nativeLibraryDirectories=[/data/app/org.nativescript.TabNavigation-d2wbgYdHKRNbKAzPZK6rVg==/lib   /data/app/org.nativescript.TabNavigation-d2wbgYdHKRNbKAzPZK6rVg==/base.apk!/lib/arm64-v8a,   / system / lib64]] 08-28 20:02:45.154 14983 14983 E AndroidRuntime:
  在   dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)   08-28 20:02:45.154 14983 14983 E AndroidRuntime:at   java.lang.ClassLoader.loadClass(ClassLoader.java:379)08-28   20:02:45.154 14983 14983 E AndroidRuntime:在   java.lang.ClassLoader.loadClass(ClassLoader.java:312)08-28   20:02:45.154 14983 14983 E AndroidRuntime:在   android.app.AppComponentFactory.instantiateService(AppComponentFactory.java:103)   08-28 20:02:45.154 14983 14983 E AndroidRuntime:at   androidx.core.app.CoreComponentFactory.instantiateService(CoreComponentFactory.java:68)   08-28 20:02:45.154 14983 14983 E AndroidRuntime:at   android.app.ActivityThread.handleCreateService(ActivityThread.java:3716)   08-28 20:02:45.154 14983 14983 E AndroidRuntime:...还有8个   08-28 20:02:45.159 14983 14983 W System.err:未捕获的异常   发生在“主”线程上。 08-28 20:02:45.159 14983 14983 W   System.err:无法实例化服务com.tns.ForegroundService:   java.lang.ClassNotFoundException:找不到类   路径上的“ com.tns.ForegroundService”:DexPathList [[zip文件   zip文件“ /system/framework/org.apache.http.legacy.boot.jar”   “ /data/app/org.nativescript.TabNavigation-d2wbgYdHKRNbKAzPZK6rVg==/base.apk"],nativeLibraryDirectories=[/data/app/org.nativescript.TabNavigation-d2wbgYdHKRNbKAzPZK6rVg==/lib   /data/app/org.nativescript.TabNavigation-d2wbgYdHKRNbKAzPZK6rVg==/base.apk!/lib/arm64-v8a,   / system / lib64]] 08-28 20:02:45.159 14983 14983 W System.err:08-28   20:02:45.159 14983 14983 W系统错误:StackTrace:08-28 20:02:45.159   14983 14983 W System.err:java.lang.RuntimeException:无法执行   实例化服务com.tns.ForegroundService:   java.lang.ClassNotFoundException:找不到类   路径上的“ com.tns.ForegroundService”:DexPathList [[zip文件   zip文件“ /system/framework/org.apache.http.legacy.boot.jar”   “ /data/app/org.nativescript.TabNavigation-d2wbgYdHKRNbKAzPZK6rVg==/base.apk"],nativeLibraryDirectories=[/data/app/org.nativescript.TabNavigation-d2wbgYdHKRNbKAzPZK6rVg==/lib   /data/app/org.nativescript.TabNavigation-d2wbgYdHKRNbKAzPZK6rVg==/base.apk!/lib/arm64-v8a,   / system / lib64]] 08-28 20:02:45.159 14983 14983 W System.err:在   android.app.ActivityThread.handleCreateService(ActivityThread.java:3721)   08-28 20:02:45.159 14983 14983 W System.err:在   android.app.ActivityThread.access $ 1400(ActivityThread.java:235)08-28   20:02:45.159 14983 14983 W System.err:在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1784)   08-28 20:02:45.159 14983 14983 W System.err:在   android.os.Handler.dispatchMessage(Handler.java:106)08-28   20:02:45.159 14983 14983 W System.err:在   android.os.Looper.loop(Looper.java:214)08-28 20:02:45.159 14983 14983   W System.err:位于   android.app.ActivityThread.main(ActivityThread.java:6986)08-28   20:02:45.159 14983 14983 W System.err:在   java.lang.reflect.Method.invoke(本机方法)08-28 20:02:45.159   14983 14983 W System.err:在   com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)   08-28 20:02:45.159 14983 14983 W System.err:在   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)08-28   20:02:45.159 14983 14983 W System.err:由以下原因引起:   java.lang.ClassNotFoundException:找不到类   路径上的“ com.tns.ForegroundService”:DexPathList [[zip文件   zip文件“ /system/framework/org.apache.http.legacy.boot.jar”   “ /data/app/org.nativescript.TabNavigation-d2wbgYdHKRNbKAzPZK6rVg==/base.apk"],nativeLibraryDirectories=[/data/app/org.nativescript.TabNavigation-d2wbgYdHKRNbKAzPZK6rVg==/lib   /data/app/org.nativescript.TabNavigation-d2wbgYdHKRNbKAzPZK6rVg==/base.apk!/lib/arm64-v8a,   / system / lib64]] 08-28 20:02:45.159 14983 14983 W System.err:在   dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)   08-28 20:02:45.159 14983 14983 W System.err:在   java.lang.ClassLoader.loadClass(ClassLoader.java:379)08-28   20:02:45.159 14983 14983 W System.err:在   java.lang.ClassLoader.loadClass(ClassLoader.java:312)08-28   20:02:45.159 14983 14983 W System.err:在   android.app.AppComponentFactory.instantiateService(AppComponentFactory.java:103)   08-28 20:02:45.159 14983 14983 W System.err:在   androidx.core.app.CoreComponentFactory.instantiateService(CoreComponentFactory.java:68)   08-28 20:02:45.159 14983 14983 W System.err:在   android.app.ActivityThread.handleCreateService(ActivityThread.java:3716)   08-28 20:02:45.159 14983 14983 W System.err:... 8更多08-28   20:02:45.188 14983 14983 I处理:正在发送信号。 PID:14983 SIG:9

我尝试删除钩子,平台,节点模块,然后再次添加。没有帮助。

AndroidManifest.xml:

enter code here
    <service android:name="com.tns.ForegroundService" android:exported="false" 

enter code here

foreground-facade.service.ts:

import { Injectable } from '@angular/core';
import * as app from 'tns-core-modules/application';
//import ContextCompat = android.support.v4.content.ContextCompat;

@Injectable({
  providedIn: 'root'
})
export class ForegroundFacadeService {
  startForeground() {
    if (!app.android || !app.android.context) {
      return;
    }
    const foregroundNotificationIntent = new android.content.Intent();
    foregroundNotificationIntent.setClassName(app.android.context, 'com.tns.ForegroundService');
    foregroundNotificationIntent.putExtra('title', 'Serwis pobierania danych z pompy jest w trakcie działania');
    console.log("start freground");
    app.android.context.startForegroundService(foregroundNotificationIntent);
    //app.android.context.startForegroundService(foregroundNotificationIntent);
  }

  stopForeground() {
    const foregroundNotificationIntent = new android.content.Intent();
    foregroundNotificationIntent.setClassName(app.android.context, 'com.tns.ForegroundService');
    console.log("stop freground");
    app.android.context.stopForegroundService(foregroundNotificationIntent);
  }
}

foregroundservice.ts:

import * as Application from 'tns-core-modules/application';

@JavaProxy('com.tns.ForegroundService')
export class ForegroundService extends android.app.Service {
  public onCreate(): void {
    super.onCreate();
  }

  public onDestroy(): void {
    super.onDestroy();
    this.stopForeground(true);
  }

  public onBind(param0: android.content.Intent): android.os.IBinder {
    console.log(param0);
    return null;
  }

  public onStartCommand(
    intent: android.content.Intent,
    flags: number,
    startId: number
  ) {
    super.onStartCommand(intent, flags, startId);
    this.startForeground(1, this.createNotification(intent));
    console.log("start foreground onstartCommad");

    return android.app.Service.START_STICKY;
  }

  private createNotification(
    intent: android.content.Intent
  ): android.app.Notification {
    this.disableDozeMode();
    this.createNotificationChannel();
    return this.getNotificationBuilder()
      .setSmallIcon(android.R.drawable.btn_plus)
      .setContentTitle('MED-LINK')
      .setChannelId('TNS-ForegroundService-1')
      .build();
  }

  private disableDozeMode() {
    if (android.os.Build.VERSION.SDK_INT >= 24) {
      const intent = new android.content.Intent();
      const context = Application.android.context;
      const packageName = context.getPackageName();
      const pm = context.getSystemService(
        android.content.Context.POWER_SERVICE
      );

      intent.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);

      if (!pm.isIgnoringBatteryOptimizations(packageName)) {
        intent.setAction(
          android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
        );
        console.log('udalo sie usunac optymaliazacje baterii');
        intent.setData(android.net.Uri.parse('package:' + packageName));
        context.startActivity(intent);
      }
      else {console.log('NIEEEE EEEEE    udalo sie usunac optymaliazacje baterii');
            intent.setAction(
            android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
        );}

      // this.wakeScreenByActivity();
    }
  }

  private getNotificationBuilder() {
    if (android.os.Build.VERSION.SDK_INT <= 24) {
      // Not Oreo, not creating notification channel as compatibility issues may exist
      console.log('udalo sie  pdalic nb');
      return new 		androidx.core.app.NotificationCompat.Builder(this);

    }
    console.log('udalo sie  pdalic nb2');
    return new 		androidx.core.app.NotificationCompat.Builder(this, 'TNS-ForegroundService-1');
  }

  private createNotificationChannel() {
    if (android.os.Build.VERSION.SDK_INT <= 24) {
      // Not Oreo, not creating notification channel as compatibility issues may exist
      return;
    }
    //const importance =
      //android.support.v4.app.NotificationManagerCompat.IMPORTANCE_LOW;
    const mChannel = new android.app.NotificationChannel(
      'TNS-ForegroundService-1',
      'TNS-ForegroundService-1',
      2
    );
    var nm = this.getSystemService(
      android.content.Context.NOTIFICATION_SERVICE
    );
    nm.createNotificationChannel(mChannel);
  }

  private getTitle(intent: android.content.Intent): string {
    const title = intent.getStringExtra('title');
    if (title) {
      return title;
    } else {
      return 'Running in background';
    }
  }

  public onStart(intent: android.content.Intent, startId: number) {
    super.onStart(intent, startId);
  }
}

在此配置上:

√获取NativeScript组件版本信息... √组件nativescript具有6.0.3版本,并且是最新的。 !更新可用于组件tns-core-modules。您当前的版本是6.0.1,最新的可用版本是6.0.7。 !更新可用于组件tns-android。您当前的版本是6.0.0,最新的可用版本是6.0.2。 !更新可用于组件tns-ios。您当前的版本是6.0.1,最新的可用版本是6.0.2。 我有错误:在路径:DexPathList上找不到类“ com.tns.ForegroundService”

但是在此: 5.1我没有错误。 请帮我弄清楚如何将我的服务迁移到最新的ns

2 个答案:

答案 0 :(得分:1)

NativeScript 6仅使用Webpack工作流,您必须将类路径添加到appComponents中的webpack.config.js数组中。

// Add your custom Activities, Services and other Android app components here.
const appComponents = [
    "tns-core-modules/ui/frame",
    "tns-core-modules/ui/frame/activity",
    resolve(__dirname, "path/to/foregroundservice")
];

答案 1 :(得分:0)

您好adam

不知道您是否正确,但是我发现必须将.ts添加到我的路径中才能正常工作。但是后来又出现了另一个错误,我正在尝试修复。

resolve(__ dirname,“ src / app / foreground.service.android.ts”)

希望这可以帮助到此结束的其他人。