我有一个名为myService.java
的服务,它将运行间隔
代码是:
public int onStartCommand(Intent intent, int flags, int startId) {
final Handler mHandler = new Handler();
mRunnable = new Runnable() {
@Override
public void run() {
Log.d("Service jalan", "beneran dah");
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
List<Contact> contacts = db.getAllContacts();
List<Contact> cont = db.auditid();
for (Contact cn : contacts)
{
int idsql = cn.getID();
if(String.valueOf(cn.getFlag()).equals("0")){
Log.d("Id", String.valueOf(cn.getID()) + " Flag :" + cn.getFlag());
sending a = new sending();
a.execute();
}
}
for (Contact cn : cont)
{
String idaud = cn.getName(); // here is getting id from sqlite
Log.d("Service get", idaud);
sendget b = new sendget();
b.execute();
}
mHandler.postDelayed(mRunnable, 120 * 1000);
}
};
mHandler.postDelayed(mRunnable, 120 * 1000);
return super.onStartCommand(intent, flags, startId);
}
在本机模块中我使用此代码创建回调:
@ReactMethod
public void startTrackService(Callback successCallback) {
Intent intent = new Intent(reactContext, myService.class);
reactContext.startService(intent);
successCallback.invoke("testing service");
}
对于JS文件,我用上面的函数调用上面的函数:
let dataAdd = NativeModules.SqliteModule
dataAdd.startTrackService(function (value){
console.log("Service callback " + value);
})
但logcat只显示I/ReactNativeJS: Service callback testing service
一次,
有一种方法可以将回调从服务发送到JS,所以我的回调会发送id
就像服务运行间隔到JS文件一样?