Hello,
I'm a beginner in mobile programming and I'm committed to a PCL Xamarin
project with Xamarin.Forms
. The application uses alarms and for this I make use of dependency injection to access the native classes that take care of the notifications. In Android
I use the class AlarmManeger
which naturally has the records deleted when the device is turned off. To restore the alarms I created a class in the Android
project that inherits from BroadcastReceiver
and triggers under ActionBootCompleted
action and would like to access the SQLite
database from the PCL
project. But when referencing the connection I get the following exception:
"BootReceiver" class (.Droid project):
namespace NutriTime.Droid.Notification
{
[BroadcastReceiver]
[IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted },
Categories = new[] { Android.Content.Intent.CategoryDefault })]
public class BootReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
try
{
//A linha abaixo gera a Exception
NutriTime.Data.RefeicaoRepository dados = new NutriTime.Data.RefeicaoRepository(NutriTime.App.DataBase);
//TODO:Manipulação dos dados
Toast.MakeText(context, "Ok", ToastLength.Long).Show();
}
catch (Exception e)
{
Toast.MakeText(context, e.ToString(), ToastLength.Long).Show();
}
}
}
}
SQLite Configuration (.Droid project):
[assembly: Dependency(typeof(NutriTime.Droid.Data.SQLite_Android))]
namespace NutriTime.Droid.Data
{
public class SQLite_Android : ISQLite
{
private string _diretorioDB;
public string DiretorioDB
{
get
{
if (string.IsNullOrEmpty(_diretorioDB))
{
_diretorioDB = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
}
return _diretorioDB;
}
}
private SQLite.Net.Interop.ISQLitePlatform _plataforma;
public ISQLitePlatform Plataforma
{
get
{
if (_plataforma == null)
{
_plataforma = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();
}
return _plataforma;
}
}
}
}
SQLite Abstraction Interface (Portable project):
namespace NutriTime.Interfaces
{
public interface ISQLite
{
string DiretorioDB { get; }
ISQLitePlatform Plataforma { get; }
}
}
Class responsible for creating the database (Portable project):
namespace NutriTime.Data
{
public class DBConnection
{
private SQLiteConnection _con;
public DBConnection()
{
var config = DependencyService.Get<ISQLite>();
_con = new SQLite.Net.SQLiteConnection(config.Plataforma, System.IO.Path.Combine(config.DiretorioDB, "nutritimedb.db3"));
_con.CreateTable<PacienteModel>();
_con.CreateTable<RefeicaoModel>();
_con.CreateTable<AlimentoModel>();
_con.CreateTable<ItemModel>();
}
public SQLiteConnection GetConnection()
{
return _con;
}
}
}
What is the best way to access SQLite data within the specific Android project?