Application crashes while reaching database from BroadCastReceiver in service in xamarin android

I am using xamarin android to read my sms when it receives. I am using EFCore to use a database in my application. for this I wrote a BroadCastReceiver and registered it with the service. when I comment out the EfCore app codes it works fine but when I rip the Db it crashes and I cannot get its exception.

my service

Service]
public class SimpleStartedService : Service
{
    public override void OnCreate()
    {
        base.OnCreate();
    }
    public SMSBroadcastReceiver SMSBroadcastReceiver;
    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
    {
        IntentFilter filter = new IntentFilter(Intent.ActionQuickClock);

        filter.AddAction("android.provider.Telephony.SMS_RECEIVED");

        RegisterReceiver(SMSBroadcastReceiver, filter);

        return StartCommandResult.Sticky;

    }
    public override IBinder OnBind(Intent intent)
    {
        // This is a started service, not a bound service, so we just return null.
        return null;
    }


    public override void OnDestroy()
    {
        UnregisterReceiver(SMSBroadcastReceiver);

        base.OnDestroy();
    }
}

      

BroadcastReceiver

[BroadcastReceiver(Enabled = true, Label = "SMS Receiver", Exported = true, Process = ".anotherProcess")]
[IntentFilter(new[] { "android.provider.Telephony.SMS_RECEIVED" })]
[IntentFilter(new string[] { "com.google.android.c2dm.intent.RECEIVE" }, Categories = new string[] { "PACKAGEIDENTIFIER" })]
[IntentFilter(new string[] { "com.google.android.c2dm.intent.REGISTRATION" }, Categories = new string[] { "PACKAGEIDENTIFIER" })]
[IntentFilter(new string[] { "com.google.android.gcm.intent.RETRY" }, Categories = new string[] { "PACKAGEIDENTIFIER" })]

public class SMSBroadcastReceiver : BroadcastReceiver
{

    private const string Tag = "SMSBroadcastReceiver";
    private const string IntentAction = "android.provider.Telephony.SMS_RECEIVED";

    public override void OnReceive(Context context, Intent intent)
    {
        //using (AppDbContext dbContext = new AppDbContext())
        //{
        //    User user = dbContext.Set<User>().FirstOrDefault();
        //}

        SmsMessage[] messages = Telephony.Sms.Intents.GetMessagesFromIntent(intent);

        SmsManager.Default.SendTextMessage(messages[0].OriginatingAddress, null, messages[0].MessageBody, null, null);
        var sb = new StringBuilder();

        for (var i = 0; i < messages.Length; i++)
        {
            sb.Append(string.Format("SMS From: {0}{1}Body: {2}{1}", messages[i].OriginatingAddress,
                System.Environment.NewLine, messages[i].MessageBody));
        }

    }

      

when i unComment AppDbContext Blocks application crash.

DbContext

public class AppDbContext : DbContext
{
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>().HasKey(p => p.Id);

        base.OnModelCreating(modelBuilder);
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        string dbFileName = "SmsSampleApp.db3";
        string fullDbPath = dbFileName;

        if (Device.OS == TargetPlatform.Android)
        {
            fullDbPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "dbFileName");
        }

        optionsBuilder.UseSqlite($"Filename={fullDbPath}");

        base.OnConfiguring(optionsBuilder);
    }

      

+3


source to share





All Articles