Reputation: 223
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.
Upvotes: 0
Views: 778
Reputation: 800
You can run adb logcat
from the command line after the emulator has started. Android will cache errors for a little while, so any crash report and stack trace is still there.
Investigating this output could point you to the root cause.
An easy way to store the logcat log to a file is this command adb logcat -d > logcat.txt
EDIT: There is also another thing you can try. While your debugger is attached.
adb shell am broadcast -a android.intent.action.BOOT_COMPLETED com.example.app
Change 'com.example.app' to your application name. This will will send the BOOT_COMPLETE message to your broadcast receivers.
Upvotes: 2