I am trying to start an app build in Xamarin that runs normally when pushed to the emulator running android 7.1 (API 25). However when I want to start a service from the app on boot the app crashes but then still seems to be running and working? Since I have no way of debugging an emulator on reboot I am clueless as to where this is going wrong.
I have the following broadcastreceiver:
[BroadcastReceiver]
[IntentFilter(new[] { Intent.ActionBootCompleted })]
public class BootBroadcastReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
if (intent.Action.Equals(Intent.ActionBootCompleted))
{
Toast.MakeText(context, "Action Boot Completed!", ToastLength.Long).Show();
Intent i = new Intent(context, typeof(BackGroundService));
context.StartService(i);
}
}
}
With the following permissions:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:label="One.Android" android:icon="@drawable/baseball">
<receiver android:enabled="true"
android:exported="true"
android-permission="android.permission.RECEIVE_BOOT_COMPLETED"
android:name=".BootBroadcastReceiver" >
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
And the following service:
[Service]
public class BackGroundService : Service
{
private Timer _checkServiceTimer;
private Notifications notifi = new Notifications();
public override IBinder OnBind(Intent intent)
{
throw new NotImplementedException();
}
public void MyDebugServiceTester()
{
int i = 0;
_checkServiceTimer = new Timer((o) => {
i++;
notifi.SendLocalNotification("BackGround Booted", "notification number: " + i.ToString(), 0);
//Log.Debug("Refreshing background service", "ammount of times: " + i.ToString());
}, null, 0, 30000);
}
[return: GeneratedEnum]
public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
{
Log.Debug("BackGroundReceiver", "Started Succesfully");
MyDebugServiceTester();
//return base.OnStartCommand(intent, flags, startId);
return StartCommandResult.NotSticky;
}
public override bool StopService(Intent name)
{
Log.Debug("BackGroundReceiver", "Stopped Succesfully");
return base.StopService(name);
}
Does anyone know why it crashes and then runs anyway? any tips or solutions are appreciated.