Layar tutorial part 2

SDK Version: 

The first tutorial, we went through the steps to create a simple layer.
In this tutorial, we are going to explain how to include actions in this layer

Parsing UTF-8 encoded content correctly from SQL through Php with Json

SDK Version: 

To get this thing working, just follow these steps:

1. First thing to do is changing the default character set of your SQL database and its tables to UTF-8.

2. Your Php file should look something like this:

  1.   $connection = mysql_connect("server_ip_address", "user", "password");
  3.   if (!$connection) die("Couldn't connect to server");
  4.   mysql_select_db("database_name", $connection) or die("Could'nt select the database!");
  6.   // This is very IMPORTANT!!!
  7.   mysql_query("SET NAMES 'UTF8'");  
  9.   $sth = mysql_query($query);
  10.   $rows = array();
  11.   while($r = mysql_fetch_assoc($sth)) {
  12.       $rows[] = $r;
  13.   }
  14.   print json_encode($rows);
  16.   mysql_close($connection);  
  17. }

3. Now you can parse these json forrmatted strings, to your application. Here's how:

SQLite subqueries

SDK Version: 

Recently we worked with big databases under android, and during that I met query optimization problems when using subqueries.

From my previous experiences with MySQL I thought, the database engine optimization is always the best, and there is no way, that multiple queries from program code is better, but I ended running multiple selects in a loop in this case for the best result.

Handling old data in new app versions

SDK Version: 

When you release a newer version of your application, on update the android system identifies from the package name and version number, that the user has an older version installed. During updating not all data of the previous version is overwritten, databases, preferences, previously downloaded/created files on sdcard or internal storage remain unchanged. If there are any incompatibility with the old version of these, is must be taken care of.

First of all, files on the external storage can be lost or corrupted due to many other reasons, so version incompatibility is just one more reason to check those files before using them.
However the internal storage is theoretically not modified by external sources, if there are version incompatibilities, you have to check the state of this files too.

How to store large amount of data

SDK Version: 

The internal storage of an android device is very limited, so if your application downloads or somehow generates a large amount of data to store, you should save it on the external storage, the sd card. Even 1mb of data takes much space compared to the whole internal capacity on most of the current devices. The sd card has a bigger magnitude, and it is even expandable.

Even when storing data on the sd card you should limit the space your application can use up. For example if you cache images for an application you should track the usage the images and when the sum size of them exceeds a given limit, delete the the oldest used one.

When developing for API Level 8 (2.2) the directory get by the getExternalFilesDir() method can be used for storing files. Files stored here will be automatically deleted when the application is uninstalled.


Database transactions

SDK Version: 

In android it is highly important to use transactions when working with databases.

First, in android database operations - especially writing - are very slow. Batching them into transactions will make them much faster.

Second, the database remains consistent under any circumstances. The database system makes sure to all the operations in a transaction take effect, or on error, rollback all of them.

If you are used to other platforms like PHP+MySQL where the code usually runs on a powerful server, witch is not likely to stop execution "unexpectedly", you can be surprised how much it affects the performance in android.
The android system can kill apps/threads/activities and so interrupt database usage, the battery can discharge or can be removed etc.

The implementation is very simple, using 3 methods in the SQLiteDatabase class:


Gzipping files on the fly part 1.

SDK Version: 

Today we wanted to test ways, to optimize the download of files to android phones.
The first way we tried, is the most obvious method of using a little less of everything (bandwith, battery, cpu, backlight, time? etc), compression.

Image source.

But what about speed? Is it worth to sacrifice the app's speed for using less bandwith? How much slower is compressing/decompressing files on a phone anyway?
Read on to find out.

Maintaining global Application state

SDK Version: 

As a possible solutions mentioned in previous article Leaving an Android application the Application object can come handy. If you want to store data, global variables that needs to be accessed from everywhere in the application, from multiple Activities, in other words is you want to maintain a global "state" of the whole application the Application object can help.

For this we must make a class which extends the class add our own methods to it, and define this class in the AndroidManifest.xml as below:

  1. An example for the Application class:
  3. public class HelloApplication extends Application {
  4.         private int globalVariable=1;
  6.         public int getGlobalVariable() {
  7.                 return globalVariable;
  8.         }

Using ksoap2 for android, and parsing output data

SDK Version: 

So the other day, I was asked to check out how we could use soap on Android, preferably with ksoap2 for android, and a public SOAP Web Service. For the latter the TopGoalScorers web service was chosen.

Is this the first run?

SDK Version: 

Ever wanted to have a different flow of actions on the second or third run of your app? I had that many times, in almost every project that I haver worked on. So here is a little snippet, that you can use to store, and check the fact, if this is the first run of your app. You can modify it easily, to suit your needs.

Syndicate content