Archive for the ‘Android’ Category

Mono Android: Working with SQLite

Saturday, December 31st, 2011

If you are using Mono for Android and need to use SQLite you are in luck since the platform fully supports this scenario.  In fact you don’t even need to do anything special, and this post also applies if you are developing for Android in Java using Eclipse.  What’s different however is Eclipse will let you copy files from your Android device, in Mono you are using Visual Studio which at this time does not have that capability.

The preferred method (but not the only one) of creating, and managing a SQLite DB in Android is to create a class and inherit from SQLiteOpenHelper, which is in the namespace ‘Android.Database.Sqlite’, you will also need to add the references to assemblies: ‘Mono.Android’, ‘Mono.Data.Sqlite’

Below is an example of where you inherit SQLiteOpenHelper and then override two methods: ‘OnCreate’ and ‘OnUpgrade’

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Database : Android.Database.Sqlite.SQLiteOpenHelper, IDatabase
{
private const string DATABASE_NAME = "MyDatabase";
private const int DATABASE_VERSION = 1;
public Database(Android.Content.Context context)
: base(context, DATABASE_NAME, null, DATABASE_VERSION)
{
}
public override void OnCreate(SQLiteDatabase db)
{
db.ExecSQL(@"CREATE TABLE MyTable (id INT PRIMARY KEY, comments TEXT NOT NULL");
}
public override void OnUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.ExecSQL("DROP TABLE IF EXISTS MyTable");
OnCreate(db);
}
}

It is important to note that OnCreate is ONLY called when the database is accessed for the very first time, subsequent calls are ignored. If the DB already exists, it is ignored, OnUpgrade is called only if the version number has been incremented, the database already exists and when the database is accessed for the first time per that instance.  Also to note is that I am taking an extremely lazy approach to upgrade where I simply drop the tables and re-create them, obviously not ideal for real world scenario because you would lose all your data.

At the top of the file I am defining two constants, DATABASE_NAME and DATABASE_VERSION which I mentioned above briefly is used for upgrade scenarios. The name is the actual name the DB file will end up being names on the Android file system.  In order to browse the file system you will need to have your Android device rooted or you will need to have a developer Android device that you obtained directly from Google.

These instructions pertain to (Windows users only, the commands will be similar however on OSX)

  1. Open cmd
  2. Type ‘adb shell’, if you get an error type ‘adb kill-server’ and try again.
  3. Type ‘su’ (for super user access, your phone must be rooted or be dev phone)
  4. Once you are in the shell, type ls (you will notice its a linux shell)
  5. Type ‘cd data/data’, it will list out all of the directories
  6. Type ‘ls data’, and find your applications directory
  7. Type cd <com.company.appname>
  8. Type cd ‘databases’
  9. Type ‘ls’ to look at your databases
  10. Note the name of your database matches your constant defined above (if everything worked…)
  11. Type ‘pwd’ (print working directory)
  12. Copy the path of this directory to Notepad or somewhere safe

To browse the database you can download SQLite Database Browser for free, and do the following.

  1. In order to browse the database your must copy the db file from your Android device onto your computer, I created a batch file for this and I run it when I need to take a look at something.
  2. Open ‘cmd’ prompts
  3. Type ‘adb pull <path of db file> <local path>’ for example ‘adb pull /data/data/com.elucidsoft.myapp/databases/MyDatabase D:\MyDatabase
  4. Open up SQLite Database Browser
  5. File -> Open -> D:\MyDatabase
  6. Whenever you need to see the new version you need to File -> Close Database, and repeat step 3, otherwise it can not copy over an in-use database file

I hope this helps you when working with SQLite on Android, it definitely helps me!

Mono Android: Using Logcat Effectively

Sunday, November 27th, 2011

In my previous post I showed how you can use adb and logcat to help debug your Mono Android application.  Well I have further refined the ability to use logcat with filters to get rid of all the cruft, with the following command you will only see Mono Android related issues for your application:

C#
1
cmd /c "C:\Program Files (x86)\Android\android-sdk\platform-tools\adb.exe" logcat setprop debug.mono.trace ActivityManager:* WindowManager:* AndroidRuntime:* MonoDroid:* dalvikvm:* mono *:E *:S

Each filter is defined by <name>:<wild card>, so this filters logcat by “ActivityManager”, “WindowManager”, “AndroidRuntime”, “dalvikvm”, “mono” and the “E”, and “S” are types of log activity which are reversed because we don’t care what they come from since we already defined that filter, but instead we want ALL types for “E” and “S” of the filters we just defined.

Just drop it in a batch file on your desktop and launch it, keep it open the entire time you do your development.  Occasionally you might lose your adb connection, if that happens just type adb logcat in the shell to restart it over again.

Update: Added MonoDroid:* to capture some missing log information…

InfiniteListView Mono Android

Wednesday, August 31st, 2011

Been working with Mono Android a lot lately and thought I would give a quick example at something that I think is very useful.  I have created a quick example of how to create an InfiniteListView using Mono for Android and the .NET Framework.

It’s pretty simple, but at the same time kind of hard to get right.  You can download the example here. It is commented in the sections that I think are relevant to describe exactly what to watch out for and how/why it is that way.

More Mono Android Debugging Tips

Friday, August 26th, 2011
  1. Install the new Mono Android release 1.0.3, which substantially improves debugging performance and various other problems.  You can read more about it, and download it from here.
  2. If you followed the instructions of my previous post, I have gathered even more information on how to improve the logcat experience.  Create a bat file on your desktop and add the following lines:
cmd /C “C:\Program Files (x86)\Android\android-sdk\platform-tools\adb.exe” adb shell setprop debug.mono.trace E:all
cmd /K “C:\Program Files (x86)\Android\android-sdk\platform-tools\adb.exe” logcat

The first command enables verbose logging for mono, note that I do not know if this will decrease performance but I can only imagine it will since it has to log more often and in more places.  If you are testing performance in your app I suggest you do not keep this enabled.  If you are testing memory usage, or you want the FULL details of every single stack trace, crash, etc than definitely keep this setting.

Now to launch logcat I simply double click the bat file, much easier….

Mono Android Debugging

Sunday, August 14th, 2011

I thought I would share a series of tips what I have found to most useful while working with Mono Android.  I will post them as I discover new ways, for now I am going to start with the most basic version of debugging on Android and its using LogCat.  To get the most out of LogCat there are several tweaks I recommend, these instructions apply to Windows since I use Visual Studio as my development IDE.

First, have a multi-monitor development environment.  I have three monitors, which is ideal since you can browse the web for help, documentation, etc. on minitor 1, do all your coding & work on monitor 2, and have your logging and other various diagnostic windows on window 3.

LogCat is a program located in the adb shell for Android, adb is a program located at Android-SDK\android-sdk\platform-tools directory.  To execute LogCat,

  1. Open a cmd prompt
  2. Change directory to the adb applications in the Android-SDK folder
  3. Type: adb logcat

For an optimal experience I suggest you tweak the cmd windows options, to do this click in the upper left hand window of the cmd window on the icon itself and choose properties. I set my cmd window properties as such and then set LogCat maximzed to the entire screen.  LogCat is realtime, so you will see every event that is occuring on your Android device which is extremely helpful.

My cmd properties:

Occasionally LogCat may disconnect, or lost connection.  Simply type adb logcat again in the cmd window to relaunch it.  If you are experiencing issues where it can’t connect load Task Manager and kill adb.exe and try again.

Mono Android Linking (Xamarin)

Friday, August 12th, 2011

As mentioned on this blog post, setting Linking to Sdk and User Assemblies for Mono Android is a good idea.  It not only reduces the overall footprint of your application, but it drastically increases performance as well.  I noticed a pretty substantial difference in both when I turned it on.  I also noticed my application crashed, it crashed because I have an assembly where I keep all of my serializable xml types.  If you take a look at Xamarin help, you will see where there are several solutions to this, in fact the blog post I mentioned also discusses a couple.

For my situation, I have an assembly that only contains serializable classes.  So the best course of action is to simply exclude this assembly entirely from Linking, you can do this easily by download MonoDevelop and installing the Android package.  Then open up your solution, go to project options for your main project (start project), select “Mono for Android Build” and set “Extra arguments” to “-linkskip=<assemblyName as you can also see in the screenshot below:

You can also do this much easier in Visual Studio by simply right clicking on your main project (start project), choose “Unload Project”, at which point it will appear grayed out, then right click it again and choose “Edit”.  This will open it up in the text editor. Find the PropertyGroup with the Condition of ‘$(Configuration)|$(Platform)’ == ‘Debug|AnyCPU’ and at the bottom add the following line:

<MandroidExtraArgs>-linkskip=MyApp.Services.Metadata</MandroidExtraArgs>

You may want to repeat this for the ‘$(Configuration)|$(Platform)’ == ‘Release|AnyCPU’ block as well so the release build will also do the link skipping of this assembly.

Still Alive and Kicking!

Wednesday, June 22nd, 2011

Its been a while since I have posted anything, just wanted to let you know that I am still alive and kicking.  Working very hard on the next application and its coming along nicely.  It is the largest application that Elucidsoft will have to offer up to this point and I am very excited about it.  I hope to have it on the market by August and hopefully sooner.  The first release of the application will be for Android, with Windows Phone to follow shortly after, to be followed up by iPhone and iPad versions.  I plan to pull this cross platform deployment to all future applications we release!

So keep an eye out, I will post something as soon as its available.