Use putExtra() and getExtra() for exchanging data between intents in Android Xamarin

Put Extra

Intent updateIntent = new Intent(this, typeof(UpdateActivity));
updateIntent.PutExtra("Version", update.LatestVersion);
updateIntent.PutExtra("Url", update.Url);
updateIntent.PutExtra("Length", length);
StartActivity(updateIntent);

GetExtra

protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);

			SetContentView(Resource.Layout.Update);


		    if (Intent.Extras != null)
		    {
                var version = Intent.Extras.GetString("Version");
                var url = Intent.Extras.GetString("Url");
                var length = Intent.Extras.GetLong("Length");
            }

		}

DownloadManager in Xamarin Android

Download Manager is a System Service that optimises the handling of long-running downloads in the background.

var download =(DownloadManager) GetSystemService(Context.DownloadService);
Android.Net.Uri uri=Android.Net.Uri.Parse(update.Url);
DownloadManager.Request request=new DownloadManager.Request(uri);
var fileName = System.IO.Path.GetFileName(update.Url);
request.SetTitle("Fan Coil").SetDescription(fileName);
request.SetVisibleInDownloadsUi(true);
request.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted);
download.Enqueue(request);
//request.SetAllowedNetworkTypes(DownloadNetwork.Mobile | DownloadNetwork.Wifi);

References :
http://101apps.co.za/articles/using-the-downloadmanager-to-manage-your-downloads.html
http://stackoverflow.com/questions/13349806/why-does-the-download-completed-notification-disappear-on-gingerbread-devices

Embed SQLite database file as Asset in Xamarin Android

At a glance:

1 – Add Sqlite.cs to your project.
2 – Add your database file as an Asset.
3 – Set your database file to build as an AndroidAsset.
4 – Manually copy the database file out of your apk to another directory.
5 – Open a database connetion using Sqlite.SqliteConnection.
6 – Operate on the database using Sqlite.

1. Add Sqlite.cs to your project.

2. Add DB as asset.
sqlite01

3. Set DB to build as AndroidAsset.
sqlite02

4. Manually copy DB out of your APK.

string dbName = "db.sqlite";
string dbPath = Path.Combine (Android.OS.Environment.ExternalStorageDirectory.ToString (), dbName);
// Check if your DB has already been extracted.
if (!File.Exists(dbPath))
{
    using (BinaryReader br = new BinaryReader(Assets.Open(dbName)))
    {
        using (BinaryWriter bw = new BinaryWriter(new FileStream(dbPath, FileMode.Create)))
        {
            byte[] buffer = new byte[2048];
            int len = 0;
            while ((len = br.Read(buffer, 0, buffer.Length)) > 0)
            {
                bw.Write (buffer, 0, len);
            }
        }
    }
}

5. Open DB Connection.

using (var conn = new SQLite.SQLiteConnection(dbPath))
{
        // Do stuff here...
}

6. Operate on DB.

References :
http://stackoverflow.com/questions/18715613/use-a-local-database-in-xamarin

Create a Database with ADO.NET in Xamarin Android

First we need WriteExternalStorage Permission

Add a reference to System.Data and to Mono.Data.SQLite

// with Android.Graphics & Android.Environment
var docsFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
var pathToDatabase = System.IO.Path.Combine(docsFolder, "db_adonet.db");
SqliteConnection.CreateFile(pathToDatabase);

// without Android.Graphics & Android.Environment
using System.IO;

var docsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var pathToDatabase = Path.Combine(docsFolder, "db_adonet.db");
SqliteConnection.CreateFile(pathToDatabase);
// with try / catch
try
{
    var docsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    var pathToDatabase = Path.Combine(docsFolder, "db_adonet.db");
    SqliteConnection.CreateFile(pathToDatabase);
}
catch (IOException ex)
{
    var reason = string.Format("The database failed to create - reason {0}", ex.Message);
    Toast.MakeText(myContext, reason, ToastLength.Long).Show();
}

// using File.Exists
if (!File.Exists(pathToDatabase))
{
    var reason = string.Format("The database failed to create - reason {0}", ex.Message);
    Toast.MakeText(myContext, reason, ToastLength.Long).Show();
}
// create a connection string for the database
var connectionString = string.Format("Data Source={0};Version=3;", pathToDatabase);
            try
{
    using (var conn = new SqliteConnection((connectionString)))
    {
        await conn.OpenAsync();
        using (var command = conn.CreateCommand())
        {
            command.CommandText = "CREATE TABLE People (PersonID INTEGER PRIMARY KEY AUTOINCREMENT, FirstName ntext, LastName ntext)";
            command.CommandType = CommandType.Text;
            await command.ExecuteNonQueryAsync();
        }
    }
}
catch (Exception ex)
{
    var reason = string.Format("Failed to insert into the database - reason = {0}", ex.Message);
    Toast.MakeText(myContext, reason, ToastLength.Long).Show();
}

References :
https://developer.xamarin.com/recipes/android/data/databases/adonet/