我正在尝试使用来自react-native的HeadlessJS与expokit(退出expo)应用程序。
我想在应用关闭时运行JS函数。但该服务已通过应用关闭。
编辑: 知道,Headless在expokit RN应用程序中根本无法工作,Android无法使用JS功能。
import ...
public class HeadlessTaskService extends HeadlessJsTaskService {
@Nullable
protected HeadlessJsTaskConfig getTaskConfig(Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
return new HeadlessJsTaskConfig(
"OfflineSync",
Arguments.fromBundle(extras),
6000,
false
);
}
}
}
public class OfflineSync extends ReactContextBaseJavaModule {
public static final String REACT_CLASS = "OfflineSync";
private static ReactApplicationContext reactContext;
public OfflineSync(@Nonnull ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}
@Nonnull
@Override
public String getName() {
return REACT_CLASS;
}
@ReactMethod
public void toggleService(Boolean start){
if(start.equals(true)){
startService();
}else{
stopService();
}
}
@ReactMethod
public void startService() {
this.reactContext.startService(new Intent(this.reactContext, AndroidService.class));
}
@ReactMethod
public void stopService() {
this.reactContext.stopService(new Intent(this.reactContext, AndroidService.class));
}
}
调用 toggleService(true / false)开始停止服务
public class OfflineSyncPackage implements ReactPackage, Package {
@Nonnull
@Override
public List<NativeModule> createNativeModules(@Nonnull ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList(
new OfflineSync(reactContext)
);
}
@Nonnull
@Override
public List<ViewManager> createViewManagers(@Nonnull ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
public class AndroidService extends Service {
private Handler handler = new Handler();
private Runnable runnableCode = new Runnable() {
@Override
public void run() {
Context context = getApplicationContext();
Intent myIntent = new Intent(context, HeadlessTaskService.class);
context.startService(myIntent);
HeadlessJsTaskService.acquireWakeLockNow(context);
handler.postDelayed(this, 2000);
}
};
}
public class BootUpReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, AndroidService.class));
}
}
<service android:name=".AndroidService" android:enabled="true" android:exported="false" />
<service android:name=".HeadlessTaskService" />
<receiver android:name=".BootUpReceiver"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
@Override
public List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new OfflineSyncPackage()
);
}
HeadlessFn = () =>{
...
}
export default function App() {
NativeModules.OfflineSync.toggleService(true); //To Start service
...
}
AppRegistry.registerHeadlessTask('OfflineSync', () => HeadlessFn) // Registering HLJ
AppRegistry.registerComponent("main", () => App);
在正常的本机应用程序中,此行为看起来不错,但在博览会应用程序中,HJS服务随应用程序一起关闭。