I have an intent service that do the following:
extracts from intent a string parameter,
composes http query using the parameter,
executes it using async task and System.Net.Http,
parses received json and puts some field values to string sqlQuery which should insert data into sqlite db,
executes sqlQuery using Ado.net (with method that open a connection, then closes it).
Note that there can be several intents calling the service, with different PutExtra values. So, activity tells to the service: "Load information for City1", "Load information for City2" .... etc.
UPD. The intent service code:
public WeatherService() : base("WeatherService")
{
}
public string sqlQuery;
protected override async void OnHandleIntent(Android.Content.Intent intent)
{
// Get extras
string wsMode = intent.GetStringExtra("mode");
string wsLocation = intent.GetStringExtra("location");
string wsProvider = intent.GetStringExtra("provider");
string wsUpdId = intent.GetStringExtra("upd_id");
//string debugMsg = "INTENT_SERVICE_OUTPUT: Mode " + wsMode + ", location " + wsLocation + " , provider " + wsProvider;
//Console.WriteLine(debugMsg);
// Generate http string
string httpQuery = "";
switch (wsProvider)
{
case "openweathermap":
httpQuery = "http://api.openweathermap.org/data/2.5/weather?&appid=1&units=metric&lang=ru"; //fake key here
if (wsMode == "code") { httpQuery = httpQuery + "&id=" + wsLocation; }
break;
default:
httpQuery = "";
break;
}
// Get weather from the provider
dynamic results = await DataService.getDataFromService(httpQuery);
if (results["weather"] != null)
{
// Compose insert queries
if (sqlQuery == null) { sqlQuery = ""; Console.WriteLine("SQL QUERY FROM NULL TO EMPTY STRING"); }
sqlQuery = sqlQuery +
"INSERT INTO [Weather] ([IND_ID], [UPD_ID], [CITY_ID], [VALUE], [SHOW], [DATE], [TIME]) " +
"SELECT [IND_ID],'" + wsUpdId + "','" + (string)results["id"] + "','" + (string)results["weather"][0]["description"] +
"',1,NULL,NULL FROM [Indicators] WHERE [CAPTION] = 'currentConditions';" +
"INSERT INTO [Weather] ([IND_ID], [UPD_ID], [CITY_ID], [VALUE], [SHOW], [DATE], [TIME]) " +
"SELECT [IND_ID],'" + wsUpdId + "','" + (string)results["id"] + "','" + (string)results["main"]["temp"] +
"',1,NULL,NULL FROM [Indicators] WHERE [CAPTION] = 'currentTemperature';";
Console.WriteLine(sqlQuery);
}
}
public override void OnDestroy()
{
// Insert data into databse
if (sqlQuery != null) { Sql.ExecQueryScalarOrNonQuery(sqlQuery, 0); Console.WriteLine("SQL_QUERY: INSERTED"); } else { Console.WriteLine("SQL_QUERY: EMPTY!!!"); }
// Update current view
// TODO
// End Service
base.OnDestroy();
So, as you can see, I try to declare a string named sqlQuery, and trying to put in it some inserts of data. But the problem is: to correctly ask http async task, I had to make all OnHandleIntent method
protected override async void OnHandleIntent()
so, OnHandleIntent is started and OnDestroy() launches immidiately, when sqlQuery is still empty!!! And only few seconds ago, OnHandleIntent-s (in my example, two) are completed.
So, how to make the following: in OnHandleIntent we get extras, generate and launch http query (using async task on not???), then we generate and save inserts for db in (for simpleness, let's do that in string sqlQuery ;) ). Then, when all OnHandleIntents are ready, we execute the final sqlQuery (once) and end the service by calling base.OnDestroy(). Thnaks in advance.