Can you please guide me how to run a background process/service while using Worklight in Android? I have looked at many places but found nothing helpful regarding this.
I'll assume you're asking about running a background process when the app is in the background. The problem is that JavaScript in PhoneGap does not run when the app is in the background. To provide background process capability you'll need to create a plugin to manage the background process.
http://docs.phonegap.com/en/2.2.0/guide_plugin-development_index.md.html
There used to be some documentation from Worklight about creating PhoneGap plugins to be used in Worklight apps but I can't seem to find it.
There is already an existing plugin to help manage background processes in Android
https://github.com/phonegap/phonegap-plugins/tree/master/Android/BackgroundService
Technically speaking, it sounds like the flow you describe requires running a plugin from the javascript code, and starting a service from within the plugin.
The async issues here could be a bit tricky - as the return "from" the plugin to the js is async, but also the thread itself (service) inside the native code.
So wouldn't it be enough to run your native code synchronously ? (this would be synchronous "relative" to the native but async "relative" to the js)
android service - http://developer.android.com/guide/components/services.html
If you're talking about JavaScript part of the application you can detach a thread using setTimeout(function(){...}, 0);. Thats the closest thing to background thread JavaScript have to provide. You can, of course, look into web workers, but that will make things a bit more complex.
Related
I want to run a method in my main.dart file from the android(Kotlin) part of my app. For this I am using methodChannels and it works perfectly fine when the app is in foreground. However, the moment the app is killed, the flutterView is destroyed and therefore I am unable to run any methods in my main.dart file.
I read that using FlutterNativeView would allow me to do what I want but I am unable to find small code snippets to help me with it.
It would be great if you could help me with this
Thanks!!
In Flutter, you can execute Dart code in the background.
The mechanism for this feature involves setting up an isolate. Isolates are Dart’s model for multithreading, though an isolate differs from a conventional thread in that it doesn’t share memory with the main program. You’ll set up your isolate for background execution using callbacks and a callback dispatcher.
For more information and a geofencing example that uses background execution of Dart code, see the Medium article by Ben Konyi, Executing Dart in the Background with Flutter Plugins and Geofencing. At the end of this article, you’ll find links to example code, and relevant documentation for Dart, iOS, and Android.
You can write the native code for Firebase realtime database. You can check this.
You should create a custom plugin and put the native code in that plugin as writing the files directly in the Flutter android folder will make the project unmanageable in the future and you can face multiple unexpected issues.
I am building an app and I want it to show up in the user's notification bar and run in the background even when it's closed. Similar to "KWGT Kustom Widget Maker".
I've tried looking for libraries that allow this, but I can only find those related to java. Are there any available for Dart/Flutter or any other workaround? I am only targeting android devices.
I used a package called flutter_background , which worked for me perfectly.
The mechanism for this feature involves setting up an isolate. Isolates are Dart’s model for multithreading, though an isolate differs from a conventional thread in that it doesn’t share memory with the main program. You’ll set up your isolate for background execution using callbacks and a callback dispatcher.
For more information and a geofencing example that uses background execution of Dart code, see Executing Dart in the Background with Flutter Plugins and Geofencing, an article in the Flutter Publication on Medium. At the end of this article, you’ll find links to example code, and relevant documentation for Dart, iOS, and Android
After searching, I guess you have two solution:
You can use flutter wrapper for both android and IOS platform, but you won't be able to run jobs often than every 15 minutes. If you want to schedule jobs more often, you'll need to write platform dependent code, using the android_alarm_manager flutter package and background_fetch for IOS.
You can trigger the correct one using the Platform function in dart.
You should use Isolates for this, also you can use android_alarm_manager or background_fetch libraries
You can use the flutter_workmanager plugin.
It's better than the android_alarm_manager plugin since it uses newer API's on Android and also works with iOS.
I'm working on building an App with Xamarin.Forms that will periodically (re: every second) potentially do something, regardless of if the application is in the foreground or background. Think something like a Timer/Clock.
I've found plenty of information on the Android side of things. It looks like prior to 8, you can run it in a service -- and at 8 or later, you'll need to run it as a Foreground Service.
e.g.
https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/services/foreground-services
From what I can tell, if I want to write any kind of controller to initialize this service (only if necessary in my client app), I'd have to add Mono.Android to the main shared library, which seems like the wrong thing to do, since the iOS version would ALSO have this dependency.
However, the iOS documentation is a little spotty when I search for it, as are best practices. I'm not sure if the correct route is to do Backgrounding -- or if that'll close things down.
e.g.
https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/introduction-to-backgrounding-in-ios
It seems that Xamarin.Forms has come a LONG way with how things work in the past few years -- even 2016 to 2017 has a number of things changing. The web is crowded with tons of different (and no longer valid) tutorials on all of this.
Right now, I have a singleton instance of a state-tracker that can be called to update every second (or other controls, i.e. skip forward one minute).
Are there any best practices for cross-platform foreground service development? My best guess at this point is to publish Messaging events from that Controller, with specific code inside the main bodies of iOS and Android projects, which will then "know" how to create the appropriate services and then call the singleton instance of that state-tracker I mentioned before (which runs every second).
Are there any best practices or anything else that have something as simple as this published? Periodic calls, while the service is "initialized," to run in the background and make calls against a state service to track/announce different states, as necessary.
Xamarin.Forms is ONLY user-interface cross platform development, and as Xamarin.Forms is based on .Net you can also use everything available in .Net for cross platform development. And that is as far as it goes out of the box. Some cross platform features not available in .Net are covered with official and unofficial plugins like Xamarin.Essentials.
To conclude unless there is a plugin for what you require (and I am not aware), you need to use the native Android and iOS functions. Also you are limited with their limitations.
There is no way to predictably run the app on iOS in the background. While there might be some workarounds like using the push notifications none of them are something that will work well in general situation.
Beginner here,I have tried getting the data using mongoDB driver which proved to be a herculean task as the app crashed every time I tried to create a connection to database.(The queries ran perfectly if executed separately). Then I tried to implement the queries on an asynctask class which ran on a separate thread in android. I feel there is a much simpler way to achieve the task at hand. The data i am trying to access is required by the UI of the app , so it should be fast.
I have referred this question but how exactly do we go about creating a web service . Can we use a framework like flask?
Please start with the following tutorial. Here is the Xamarin version. There are also other version of this tutorial, depending on the technology stack relevent:
.NET
Node.js
Java
Python
Build mobile applications with Xamarin and Azure Cosmos DB
So here's the background story:The phonegap application is an online e-store has a plugin (which my team has created) to print receipts after the orders has been completed. All is working good but the printing is taking too long on Phonegap. In fact, it is taking 5-6 seconds per print, which is not good -- the client expects a much faster process of printing, somewhere down to 1-2 seconds.However, this is not possible using Phonegap, so I had to resort to using native applications. I created a native app with the help of a sample code. Eureka! The printing took only 1-2 seconds!On a side note, both Phonegap and the Native App are using the same Printer library -- the code for printing is entirely the same, just that the Native App's performance is better than Phonegap's.Here is where the problem lies:1. The third-party native app should ONLY run in the background (Hence Android Background Services)2. Phonegap should be able to pass the order data to this third-party native app's Android Background Service so that it will be able to print the receipt.So the question is: How do I do this?What I've currently tried is that I used a phonegap plugin (Link). It works, however, it opens the third-party app, which isn't good at all. I can close it immediately after it opens, but what the client wants is that it doesn't open up at all, to avoid confusion with their customers.Any help is greatly appreciated! If there are any clarifications on what needs to be done, please add comments. Thanks!
I fear that it is not possible to create a background service in cordova/phonegap but you can implement you supporting (Native)app with BroadcastReceiver or IntentService which(Runs in backgroun without UI) gets triggered by the (explicit)intent which you call from the cordova plugin.
More on < Service (Deep Guide), IntentServices, BroadcastReceiver > (Native) and invoking intent using cordova webIntent (plugin).
I guess this tutorial may help you.
Check this article explaining js to native code calling (or plugin development).
Create one test app, which will implement all steps explained in above article. Once implemented, you will get to know how js to native code call is executed.
Now, add printer library to your code (native wrapper), then add your code for printing (in one function, again in native code). Call this function from js. That's it.