Android strict mode preventing network IO - java

I have been developing an android application that needs to retrieve a JSON file from dweet.io, an IOT machine-to-machine communication site. With some quick google searches I found a java wrapper for interfacing with dweet, but although it seems to work fine in theory, the Application throws some errors involving a 'Strict Mode' when it tries to reach the site. This appears to be some method of optimization standard put in place by google that prevents me from putting disk or network I/O in my Main Activity. If I can't place it in my main activity, where should I put it and how should I do that?
edit: it appears the solution involves something called 'AsyncTask', but I'm not sure how to use it or what that would entail.

Create a MyTask class, which extends AsyncTask.
Override the doInBackground() and maybe other functions on, need, but not required for now.
Take a look at thistutorial.

Related

How to read from AsyncStorage in native code (Java, ObjectiveC/Swift)

I have a background service I wrote in native (Java/ObjectiveC) that needs to access data from AsyncStorage in react native. I would like to know if there is an easy way to get this value directly from the native code instead of having to pass it manually through a bridge method.
Since this is a background service that runs even if the app is not running (in Android), it can not call the Javascript code when it needs this data. I could work around it (by setting it in native at the same time I set it in AsyncStorage) but it would be a lot more work.
If you meant this one: https://reactnative.dev/docs/asyncstorage then, it's deprecated. I recommended react-native-default-preference: https://github.com/kevinresol/react-native-default-preference
On Android, it used SharedPreference, so if you use the same preference name. You should able to read it on Android native code
You are going to need to configure the react-native bridge. Here is a good link that should help you understand and configure the bridge.

Testing performance of different code

Usually when I create something I have a number of different ideas on how to do it. Anytime I'm not sure which is better, rather than posting here each time, I'd like to test things myself. What methods can I use to test speed/performance/memory usage etc in Android?
While I personally have little experience with them, Performance Tips offered by the Android Documentation gives mention to two potentially useful programs for checking benchmarks.
Upon a cursory glance, it seems Traceview is useful for testing individual methods as well as broad scopes of an application.
Based on the documentation, creating a trace, retrieving it, and processing it can be done as follows:
First, find the locations where you want to test performance. Here's a quick sample:
public void doSomething() {
//maybe a long process that needs optimizing
}
protected void onCreate(Bundle savedInstanceState) {
// set up stuff
Debug.startMethodTracing("myfilename");
doSomething();
Debug.stopMethodTracing();
}
Second, retrieve the file from your Android device (note, you need a write-permission to create this file, so be sure to add that to your manifest). This can be done using the Android Debug Bridge (located in <Android SDK Home>/platform-tools). Open a command shell and run the following command
<Android SDK home>/platform-tools/adb pull /sdcard/myfilename.trace <Desired Location>
Lastly, you can process the output using Traceview with another command call. Traceview is located in the <Android SDK Home>/tools directory, so the call would look like this:
<Android SDK Home>/tools/traceview <Desired Location>/myfilename
Note that depending on your operating system, you may need to switch forward slashes with back slashes. This is a rough outline of the process outlined in the documentation (cited above), so I would recommend reading that before trying to implement this.

Android App code design message handling for commands

I'm having two main code design problems in my app.
My app mainly consists in sending ssh commands to a remote host.
Right now I have a separated thread (singleton) which gets messages through the handler which specifies which is the next command to be sent, or the username/password/ip (kind of messy but works...).
This approach works good for unidirectional commands, but I'm planning to make it bidirectional which I don't know how to implement. As far as I know Android doesn't allow to change UI elements by another thread so a listener pattern wouldn't be it.
Also, I just read that we shouldn't save things in the application object, which is also what I'm doing by saving whether my app is running full or lite mode... I don't know where should I save it in order to not make it obviously hackable (sqlite-SharedPrefs are easily editable...)
Only a general hint: There is Activity.runOnUiThread() to execute code (later) on main thread.

JSoup works in an Android Activity, but not as an object in the Activity

I have created an Android app that I would like to supply with some info scraped from a page. I used JSoup, and was succesfully able to scrape all of the HTML, and place them into the proper data structures in a console project, with a single class name Scraper.java.
My next step was to port this Scraper.java into my Android app. To do this I simply wanted to make a Scraper object in my activity that I need the html info for. This didn't seem to work, and I was getting an error that seems as if it was trying to tell me that Android didn't like this external class trying to connect to the internet.
Giving up I very dirtily dumped all of the code from Scraper.java into my Activity class and got it to scrape the data fine, and it connected without any issue.
The problem now is that I realize I want to access that complex data in other Activities, and it really would be much simpler for me to just go back to trying to have an external Scraper.java file so I can share an object of it rather than all of the data types inside of it.
So my question is, what do I need to do to let Android know that it doesn't need to freak out, and can let my external Scraper.java file connect.
I have allowed the permission for internet in the manifest so I am lost as for what to do from here.
I found out that I had a very silly error, and my code is functional now. Sorry for the long delay for my response I was very busy last night.
The problem that I had was when I ported over my console version I still left in a main method in the Scraper.java file which was clashing with my Android app. I cleaned up the code even more, and am able to reference a Scraper.java object very easily.
To remedy sharing an instance of an object once I have created I think I am going to implement Parcelable with my Scraper.java file so that I can share the instance easily amongst multiple activities. For anyone that is interested I will be referencing http://prasanta-paul.blogspot.com/2010/06/android-parcelable-example.html

Is there a way to sandbox parts of a java process on Android?

I want to use an SDK in android, but I don't want that SDK to have any access to permissions that I haven't explicitly given it. On top of that, if the SDK throws an uncaught exception, I don't want it to bring down my entire application.
In C# there is the concept of an AppDomain where you can treat the code running inside of it like a sandbox, granting (or limiting) permissions, and only providing access to data that you explicitly want to share.
Is there anything that works like this in Java for the Android platform?
There are two questions here. The first one deals with handling exceptions in a piece of untrusted code; a carefully written try/catch block should take care of that, as long as the untrusted piece is pure Java. If native code is allowed, then nothing short of process level isolation would help. For running code in a separate process, read up on Android services; you can designate a service to run in a designated process. Then exceptions (the nonmanaged kind) won't bring down the main app. You'll just get a "service has died" exception.
A whole another issue is lowering the permission set. To the best of my knowledge, there's no way to lower the permission set within an app. Even a surrogate process won't help. If you ship a whole application (in the Android sense of the word) for wrapping and running custom code, that might help. But the logistics of app installation would get tricky. Google Market does not readily support the notion of app interdependence or prerequisites.
All the permissions you give to your app are the permissions that are allowed to otherwise it wont have permissions for almost nothing.
you set your permissions in your Manifest.xml otherwise than that you cannot set other kind of permissions.

Categories