If something happens in my app I want to kill the app there and then. In swift I can use fatalError(), is there a java equivalent?
I don't there's any method equivalent to fatalError in Android.
If you want to kill the entire process then you can use android.os.Process.killProcess(android.os.Process.myPid());.
To mimic the same behaviour of fatalError you'll probably have to implement it yourself.
The behaviour you'll need to add besides killing the process is
to log a message, you can to this by using Log
print the file and line number, for this you can follow this thread on SO
Kotlin's Preconditions.kt file includes an error(message: Any) function that, in turn, throws an IllegalStateException[1].
As long as you're not swallowing the exception, it should kill the app much like fatalError().
[1] Source code
Related
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 am trying to debug a Nativescript app for Android and I have noticed that it no longer prints Syntax errors with their corresponding line and col. numbers, not even console.log statements. How can I enable this feature again? The command I use to launch the app is tns run android. I am using Nativescript version 2.5.2. The VSCode plugin doesn't work either as it ignores any breakpoint I place, and also the console doesn't show any errors. What can I do?
Edit:
It appears that only console.error() statements get printed.
After tinkering a lot with different kinds of constructions and by researching more into how Nativescript relates to regular browser JS, I found out that the problem lies in the use of Promises, which in case of errors defer the execution to the catch() method, but unlike any other regular programming language, "unhandled" rejected results within promises won't raise any kind of exception; which means that, basically, for any promise you invoke in your code, you should have a chained catch() method call so you effectively catch any errors your promises might produce. This also applies to promises which involve navigation, where stuff gets a lot trickier: You might think the next view (say, view.js) will have a call stack on its own and produce an unhandled exception at code that's not even inside a promise in there, but that's not the case: An exception produced at view.js will get captured by the catch() method of the promise within which you started the navigation, and any subsequent promises must have their own catch() method calls because errors won't get bubbled up to the previous view. I can think of many other troublesome constructions but I hope the important bit stays clear: always chain a catch call on any and all promises.
Is there a mechanism for observing changes in system properties from your own app in Android SDK?
For example, I want to be able to detect changes in the system.adb.tcp.port property.
As of API 25 there's no public API for this. You need to dig down into internal functions that only exist to exercise the system.
See __system_property_wait_any() in sys/_system_properties.h. The need to define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ shows that its use is even discouraged internally.
I wouldn't use this in an app. A quick grep of AOSP indicates that it's only used in test code, so there doesn't appear to be a "friendly" API that makes use of it.
I think you're stuck with polling System.getProperty().
I don't think exist a mechanism to do this., but you may be able to retrieve system properties by executing getprop in a Process and retrieve its output.
or try this link : [for System.getProperty()]
http://developer.android.com/reference/java/lang/System.html#getProperty%28java.lang.String,%20java.lang.String%29
For C/C++, please call WaitForProperty defined in "android-base/properties.h" and libbase.
For Java, please call OnPropertyChangedCallback in class Observable.
I have a unity app that should run on android. I have some native code (inside a plugin) that I need to call in order to process deep-link activation of the app, and I need to call it from my unity script when the app gets reopened by that deep link. Where is the appropriate place to make that call?
Note OnApplicationPause is not a good option, since it mimics onResume, and you MUST NOT process deep links inside onResume on Android because it will cause them to get re-processed over and over every time the app is reopened after the initial click on the deep link.
So basically, I am looking for a lifecycle-equivalent to onCreate, but in unity script (I do not have legal permissions to replace the native unity activity in this case and simply override its onCreate method).
onCreate() is run basically on the initialization of any activity. It's kind of hard to think about it in terms of Unity3D. When you call a level the Start() and Awake() are called, the latter first. You could potentially UnitySendMessage() from your native plugin on the java Android side. I believe the syntax looks like:
UnityPlayer.UnitySendMessage("GameObjectName1", "MethodName1", "Message to send");
And inside your GameObjectName1 construct MethodName1 method with string params passed:
function void MethodName1 (message:String) {}
Not sure if this will suffice for your needs. I've done comparable with iOS, i'm sure there should be an Android equivalent with Unity3D.
I'm currently trying to figure out how to do the following:
I want to make a service which will log all the activities / applications (names, their start time, end time and exceptions) also log where the user touched the screen. Is there a way to do it without parsing LogCat ?
Any ideas?
You could build a string of events that occur in each stage of your program, and then write them to a file in the overridden onPause, onDestroy, etc. methods.
see: http://developer.android.com/guide/topics/data/data-storage.html
Either that or learn how to use the intent filters on adb! There are a few tutorials you can use for this.