IdHTTP in Android service in Delphi RX - android

I try to write an Android service, which will use IdHTTP component to send messages to the server.
I just put IdHTTP1 on TAndroidServiceDM and try to Run app on Phone (Android 4.2.2)
I get error:
"Project LocSensDemo.apk raised exception class Exception with message 'Activity not found, maybe you are in a service'"
in FMX.Platform.Android line 1792 "Activity := TAndroidHelper.Activity;"
and then
"Project LocSensDemo.apk raised exception class Segmentation fault (11)".
Service crashes.
How to solve this problem?

The Activity not found exception occurs when the TAndroidHelper.Activity property is accessed inside a service project. That property is not usable in a service (see RSP-13381). Anything that uses TAndroidHelper.Activity in a service will crash at runtime. Line 1792 in the FMX.Platform.Android unit is inside of FMX.Platform.Android.TWindowManager.RetrieveContentRect(). What are you accessing in a service that is using TWindowManager? Services don't have UIs, you should not be touching anything UI-related to begin with.
If your service code needs access to an Android Context object, such as to call getContentResolver(), you can use the TAndroidHelper.Context property, or your service's own JavaService property (Android's Service class derives from Context).
As for the Segmentation fault exception, you haven't provided any details about that error, or shown what code is causing it, so there is no way for anyone here to diagnose it for you.

Related

ServiceManager.getService returns null

Im trying to develop an app that checks your device's security, and im working on a feature that checks if the pattern is visible (In the lock screen, if u have a pattern and it shows on the screen).
Some versions ago, you could check this in Settings.Secure with:
Settings.Secure.getInt(contentResolver, Settings.Secure.LOCK_PATTERN_VISIBLE)
They changed it and now it is on a class called LockPatternUtils, and the method is called isVisiblePatternEnabled.
The only way to access this class is through reflection, but when I do it, it throws InvocationTargetException caused by NullPointerException.
I tried to debug but got "source code does not match byte code", so I checked the source code in Android Code Search, and it throws NullPointerException cause inside this method, they use ServiceManager.getService("lock_settings"), and that specific part of code returns null.
It says that getService returns "a reference to the service, or null if the service doesn't exist".
The thing is that this service DOES exist, as when I execute the adb command to check a list of services that are running on the emulator, it appears.
So, here is my question: Is there anything that I'm missing?
I know this is a very old query, but I hope this helps.
Even with reflection, only system apps are allowed to get the 'lock_settings' service. This makes sense as these are secure features.

Kotlin/Android app crashes without a Stack Trace

I've encountered a situation where anything that runs in a coroutine (database queries, network requests or response processing) and fails at some point, simply crashes the app without any logs at all. Is it the expected behavior or do I have something misconfigured?
Here's a simple piece of code that reproduces the error.
Nothing changes with different Dispatchers.
This is the only output I'm getting, followed by app termination
And these are the dependencies I'm using, the latest version at the moment of writing.
It's also reproducing in a simple CLI app without the Android framework.
Edit: Just to make it clear. I'm not trying to see the Exceptions that I'm throwing. I'm trying to get some output when my app crashes because of an unknown reason.
Edit2: To add some clarity, here is another example of the situation without any "throw" statements.
Since the exception in happening inside the coroutine, it makes sense to find it there also. Just surround it with try/catch and you will see the magic:
GlobalScope.launch{
try{
print("Your printing message")
throw Exception("Message here")
}catch(exception: Exception){
exception.printStacktrace()
}
}

Report all Android crashes to own API

I am in a situation where I cannot use third party Crash reporting platform (Crashalytics ...etc)
So, I started to use self developed Rest API to receive crash reports from mobile app.
However, I can only send handled crashes so far by using try-catch
How can I handle all crashes thrown by my mobile app even those which are not in my try-catch clauses
Just like Crashlytics which can catch all crashes and sends them to Firebase without placing try-catch
Have a look at Thread.UncaughtExceptionHandler
from the docs :
When a thread is about to terminate due to an uncaught exception the
Java Virtual Machine will query the thread for its
UncaughtExceptionHandler using Thread.getUncaughtExceptionHandler()
and will invoke the handler's uncaughtException method, passing the
thread and the exception as arguments. If a thread has not had its
UncaughtExceptionHandler explicitly set, then its ThreadGroup object
acts as its UncaughtExceptionHandler. If the ThreadGroup object has no
special requirements for dealing with the exception, it can forward
the invocation to the default uncaught exception handler.
using this, you can get all threads and all exceptions, which you can then report to any API you'd like
you can find examples of how it was implemented
here and here
There is a library which you can use called ACRA , its an open source library you can check its code , or you can directly use their library and configure it to hit your api
#AcraHttpSender(uri = "http://yourserver.com/yourscript",
basicAuthLogin = "yourlogin", // optional
basicAuthPassword = "y0uRpa$$w0rd", // optional
httpMethod = HttpSender.Method.POST)
public class MyApplication extends Application {

Send report after Android JNI signal/exception

I am trying to get firebase working with my Android app but it is mainly C++ code.
A lot of chances are that if there is any crash it will be some kind of bad access in the C++ part.
Firebase works well with uncaught java exceptions however I cannot get it to work with JNI signals/exceptions.
As far as I know it is not yet compatible with JNI but I thought a workaround would be something similar to this:
Somewehre in the C++ add a signal handler for signals we would like to handle that will send it back to the Java side and try to send a report ( with part of the stacktrace if possible ).
#include <cisgnal>
namespace
{
void SignalHandler( int sig )
{
// Code to call a static method in my Activity
}
}
CrashReporter::CrashReporter()
{
::signal( SIGABRT, & ::SignalHandler )
}
// In java
public static void SendReportOnCrash()
{
FirebaseCrash.report( new Exception( "OOPS" ) );
}
Unfortunately, fake reports are never sent, however I do get callback in Java.
I tried to launch a process separated activity in which I would call FirebaseCrash.report() but there is no non-static way to it therefore it always crash since FirebaseApp/Crash are not instantiated in secondary activity.
I come here to ask if someone would have a hint on how to do that.
My last try but least wanted test would be to write the stack trace to a file, and upon a new start, test if this file exists, if so use FirebaseCrash.log then send a fake report...
You are not guaranteed to be able to do any Java processing after the JVM calls abort() for a fatal error. Per the Java documentation:
SIGABRT
The HotSpot VM does not handle this signal. Instead it calls the abort
function after fatal error handling. If an application uses this
signal then it should terminate the process to preserve the expected
semantics.
Yes, that is for the Oracle implementation. It quite likely applies to all other implementations also.
Because at the point it calls abort(), the JVM expects to be killed.

Call to JNI crashes after android service is restarted?

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.

Categories

Resources