I am using JNI in my application... When application launches it works fine and JNI logs are getting printed .. But once application went background and comes foreground after using some other application JNI Logs are not printing and methods are not getting called..
Can anyone suggest me a solution?
Thanks in advance..
Would you give more specific info about your JNI design?
First thing need to check...
Are you building JNIcode as shared library.So Check in logs JNI_Unload is getting called when Application is going in background.
Second thing need to check...
Check in Application going in backgroud ..Shared Library getting unloaded by Application.
Third Thing need to check...
Your JNI code is caching properly jvm and JNIEnv variable.
Related
I have been trying to imlement in my app a way to run some task when app is "closed" or "breaked", I have created a BroadcastReceiver to do this, but I have notice something that could be logical. In my Broadcast receiver I am using classes from my shared project or PCL, but when app is breaked I have noticed that the only class that still exists is the BroadcastReceiver one, so when I do call a class that is in my shared project in my broadcast it gives me an error, I have conclude this beacuse when my app hasn't been breaked the execution to call the classes of the shared project is OK, but when breaked... not,
I have two questions for this problem:
1) Is my theory correct?
2) If is correct (or not) how can I call from my BroadcastReceiver classes that are in my shared project when the app is breaked?
Here is some sample of my code:
//Android
override void OnReceive(...)//BroadcastReceiver main method
{
...
MyClass myCls = new MyClass();//Class from shared project
}
When I execute this when app is still in use everything of but when is breaker then the app stops and shows me the following typical android pop up:
“MyAplication.Drioid has stopped”
Do you want to let your app restart again after the app is closed? If yes,for android, you can try to use service to achieve this.
When the service is in the background, you can try to restart it by using a BroadcastReceiver which will receive a signal when someone or something kills the service, its role is to restart the service.
For more details, you can refer to: https://fabcirablog.weebly.com/blog/creating-a-never-ending-background-service-in-android
Note:Even though it's Java code, it's easy to understand and convert.
For xamarin Form, because each platform has their own way and rules for background services. You have to make services for each platform.
You can refer to this document: Xamarin Background Tasks
Assumptions
I have an application with a very large number of screens.
I see the application for the first time so I don't know it
I have a method that connects to the Web Service and I don't know if it is actually used.
Is there any way to check (without running the application) whether the method is actually used in the application?
In Android Studio, you can easily find if a method is used or not.
Right click on the method > "Find usage"
Put a breakpoint on method and start debugging to know whether control
transfers to that method.
Put Log statements inside method to know whether they are executed.
Not recommended: Use Toast messages.
If I understand you, you're looking for "dead code", right ?
You can use Android Lint then: Analyze > Inspect Code...
In the results, Java part I think, you will find Declaration redundancy which will point you the dead code of the project (or the analyzed package)
I've just started work with Koltin and my question might be a little strange to someone who have more experience,
but how can i see fatalExceptions in logcat? For example, i have an app that is already developed by another dev,
there is an error in one activity - after pressing the button apps crash and restart to main activity.
I don't see any usefull informations in logcat(in fabric also!), moving on trough whole code from listener to fragment and many classess is very time consuming. There must be some way to figure it out quicker, right?
Exceptions should be shown/thrown in logcat, same as with Java.
If the exception is thown within rxjava or a kotlin coroutine, make sure you have defined an error handler, otherwise the exception might get swallowed.
Then make sure you have selected the right app in logcat and that no filter is active.
Also make sure there is no other global Exception handler defined besides fabric.
I have been working on an Android app using Phonegap and now would like to make it so when the app is closed it can still execute the java/js code in the app. So I understand I need to create a service. If I create a service plugin on phonegap can I still execute the javascript code or only the java?
Has anyone does something like this? I found this discussion but did not seem to work: http://groups.google.com/group/phonegap/browse_thread/thread/722b0e796baa7fc6
So that is all I have right now.
Before I turn to developing it native if I can't figure it out thought I would ask if anyone has done this before. I can't seem to find any of the phonegap plugins that do something similar.
EDIT: I have got an app that executes Java code as a service. However when it calls sendjavascript it does not work. So is there a way to have the javascript code running in the background as well when an app is closed with phonegap?
Thanks
No, it is not possible to run Javascript code in the background (at least in my opinion) as a service. Phonegap on Android uses an special activity called Droidgap, which hosts a WebView. This browser control executes the JavaScript. This means that JS execution can only handled inside this activity, regardless if it is visible or not.
The code you linked from Google Groups tries to bind a service developed in Java to the DroidGap activity, so the service is NOT written in JS.
You can have some background activity within your JS code inside your child activity derived from the DroidGap activity. For example have a background thread in your activity, have a JS callback function and let the thread call this callback functionality.
If you really need a service you have to go native.
Update:
JS code can only be executed with the Droidgap activity. An activity can have 3 states (based on the Lifecycle of activites):
visible
invisible but still loaded
not loaded
I provided a sample in which I implemented a Phonegap plugin. The plugin allows the activity to register itself to SMS_RECEIVED. When the activies goes out of scope (event onbeforeunload), it deregisters, so only issue 1 is handled.
When you want all 3 issues handled, you have to forward the incoming SMS intent to the activity. When it is not loaded the system will automatically load and activate the activity. But this is not a background service anymore, your app would become visible whenever a SMS is received.
If you don't want this (if you really want a background service), you have to provide a native implementation.
There is this article on how to create a service on Android with Phonegap which gives some good information on your problem.
It's using a great plugin in order to build a background service with phonegap easily. But you can't use JS though
I didn't find a way to make JS to run in the Background. BUT you can pass parameters from Java to JS and vice versa with the plugin...which is pretty useful.
You would still need to rewrite your JS code in Java though.
Unless you do have a specific reason to only want JS to be run? (But there shouldn't be...)
Hope that could be useful to some people visiting this page.
YES, and it is very simple... just install the plugin backgroundJS:
https://build.phonegap.com/plugins/430
It allows you to run javascript on the background and combined with the local notification plugin, you can even send notifications to the user at any time, just keep in mind that doing this will cause the battery to run out faster, also consider that this might create a problem with the iOS policy. good luck!!!
You can try to add plugin cordova-plugin-background-mode
But as author says:
Infinite background tasks are not official supported on most mobile operation systems and thus not compliant with public store vendors. A successful submssion isn't garanteed.
Use the plugin by your own risk!
I have an android service that uses a native library via NDK/JNI. The native library is statically loaded/initialized as suggested...
static {
System.loadLibrary("mylibrary");
if (!nativeClassInit())
throw new RuntimeException("native init failed");
}
Everything works fine until the service is stopped (e.g., from a UI activity where the service may be started/stopped). The problem is when the service is stopped the task has not yet been killed by android and if the user restarts the service the native libraries are not reloaded and a call to a native function causes a crash in the native code?
I tried calling the nativeClassInit() method again after a restart but this doesn't help?
Also, I seen in other posts that unloading the native library is not allowed in Android.
I found by making the service run in its own private process and having the service kill itself in its onDestroy() method via android.os.Process.killProcess() solved my problem. Not sure if this is the most correct way but I would be happy to hear any suggestions.
Concerning the above comments...
What is the native code trying to do when it crashes, and what is the error?
The native code crashed as soon as an internal native library function was called just before the crash an __android_log_print was done displaying the correct parameter values passed in. Its hard to say what exactly happened from the dump.
Do you preserve some JNI pointers (JniENV, jobject-s, etc.) on native side between Service restarts?
Yes, I preserve the JVM in the Onload method and also JNI class IDs & method IDs in my static initialization method. I tried recalling the static initialization method but the same crash occurred.