1
Hello,
I’m a beginner in mobile programming and I’m committed to a project PCL Xamarin
with Xamarin.Forms
. The application uses alarms and for this I use dependency injection to access the native classes that take care of notifications. No Android
use the class AlarmManeger
which naturally has the logs deleted when the device is turned off. To restore the alarms I created a class in the project Android
who inherits from BroadcastReceiver
and fires on action ActionBootCompleted
and would like to access the database SQLite
from the project PCL
. But when referencing the connection I get the following exception:
"Bootreceiver" class (project . Droid):
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 (project . Droid):
[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 Design):
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 most suitable way to access Sqlite data within the specific Android project?
The code shown does not help identify the problem, show the configuration code of the
SQLite
within the projectAndroid
. For more details see in this question: http://answall.com/questions/126353/xamarin-dependencyservice-null @Dantas-j– rubStackOverflow
@rubStackOverflow you are talking about the implementation of Sqlite in the android project?
– Dantas J
Exactly @Dantas-j
– rubStackOverflow
Also put the code where you are instantiating
SQLite_Android
. When you aredebugando
where exactly the error occurs?– rubStackOverflow
@rubStackOverflow the error occurs in the Bootreceiver class that runs automatically when the device boots. Error line specified (line 13).
– Dantas J
Later I will compare with my code and try to help. @Dantas-j
– rubStackOverflow
Does this code run when the phone is turned on correctly? Running a simple test (without needing to restart) does the connection to the bank work? @Dantas-j
– rubStackOverflow
@rubStackOverflow The connection to the database works perfectly in the classes of data manipulation (the Models) in the Portable project. In the Bootreceiver class I can’t run the code without restarting the device :/
– Dantas J
Correcting: in the classes of data manipulation (the Viewmodels)
– Dantas J