i looking forward switch app rect-native native. have app track user's gps location when application closed. how can achieve in react native.
i had headless js , tried following , doesn't work
in manifest
<application> ... <service android:name="com.backgroundservice" /> ... </application>
index.android.js
appregistry.registercomponent('geolocation', () => geolocation); appregistry.registerheadlesstask('sometaskname', () => require('./sometaskname'));
sometaskname.js
import socketioclient 'socket.io-client'; module.exports = async (taskdata) => { // stuff var socket = socketioclient('http://xxx.xx.xx.xx:4000'); socket.emit("test"); }
backgroundservice.java
package com; import android.content.intent; import android.os.bundle; import android.support.annotation.nullable; import com.facebook.react.headlessjstaskservice; import com.facebook.react.bridge.arguments; import com.facebook.react.jstasks.headlessjstaskconfig; public class backgroundservice extends headlessjstaskservice { @override protected @nullable headlessjstaskconfig gettaskconfig(intent intent) { bundle extras = intent.getextras(); if (extras != null) { return new headlessjstaskconfig( "sometaskname", arguments.frombundle(extras), 5000); } return null; } }
mainactivity.java
package com.geolocation; import android.content.intent; import android.os.bundle; import android.os.persistablebundle; import android.support.annotation.nullable; import com.backgroundservice; import com.facebook.react.reactactivity; public class mainactivity extends reactactivity { @override public void oncreate(@nullable bundle savedinstancestate, @nullable persistablebundle persistentstate) { super.oncreate(savedinstancestate, persistentstate); intent service = new intent(getapplicationcontext(), backgroundservice.class); this.startservice(service); } /** * returns name of main component registered javascript. * used schedule rendering of component. */ @override protected string getmaincomponentname() { return "geolocation"; } }
you don't pass data service intent when start it,so in backgroundservice.java
, extras
null
, , gettaskconfig
method return null
. see headlessjstaskservice.java, no task start when gettaskconfig
return null.
Comments
Post a Comment