gpt4 book ai didi

java - 带有 Headless JS 的 Android 后台服务无法正常工作

转载 作者:行者123 更新时间:2023-12-05 00:05:22 35 4
gpt4 key购买 nike

到目前为止我已经实现了:

主应用程序.java :

    @Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
initializeFlipper(this); // Remove this line if you don't want Flipper enabled

Intent service = new Intent(getApplicationContext(), MyTaskService.class);

getApplicationContext().startService(service);
}

MyTaskService.java :

package com.myApp;

import android.content.Intent;
import android.os.Bundle;
import com.facebook.react.HeadlessJsTaskService;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.jstasks.HeadlessJsTaskConfig;
import javax.annotation.Nullable;
import com.facebook.react.bridge.WritableMap;

public class MyTaskService extends HeadlessJsTaskService {

@Override
protected @Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) {

Bundle extras = intent.getExtras();
WritableMap data = extras != null ? Arguments.fromBundle(extras) : null;
return new HeadlessJsTaskConfig(
"SomeTaskName", // Use the registered headless Task here
data,
5000);
}
}

AndroidManifest.xml:

  <service android:name="com.myApp.MyTaskService" />

Index.js(React-Native 代码):

AppRegistry.registerHeadlessTask('SomeTaskName', () => SomeTaskName);

SomeTaskName.js(RN代码):

    module.exports = async (taskData) => {
alert('test');
console.log('Went to background');
}

使用此代码,它会运行但会立即关闭应用程序并且不会显示任何错误。知道为什么会发生这种情况以及为什么我不能调用警报和 console.log 吗?

最佳答案

你错过了这个电话

HeadlessJsTaskService.acquireWakeLockNow(context)

根据文档要求:

If you're starting a {@code HeadlessJsTaskService} from a {@codeBroadcastReceiver} (e.g. * handling push notifications), make sure tocall {@link #acquireWakeLockNow} before returning from * {@linkBroadcastReceiver#onReceive}, to make sure the device doesn't go tosleep before the * service is started.

关于java - 带有 Headless JS 的 Android 后台服务无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59858850/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com