Xamarin安卓应用程序在启动时与BroadcastReceiver崩溃但是启动了吗?

问题描述 投票:0回答:1

我正在尝试在Xamarin中启动一个应用程序构建,当推送到运行android 7.1(API 25)的模拟器时,它正常运行。但是,当我想在启动时从应用程序启动服务时,应用程序崩溃但仍然似乎仍在运行并正常工作?由于我无法在重新启动时调试模拟器,因此无法解决这个问题。

我有以下广播接收者:

[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); 
        }
    }
}

具有以下权限:

<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>

以下服务:

 [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);
    }

有谁知道为什么它崩溃然后运行?任何提示或解决方案表示赞赏。

android xamarin crash broadcastreceiver boot
1个回答
0
投票

您可以在模拟器启动后从命令行运行adb logcat。 Android会暂时缓存错误,因此任何崩溃报告和堆栈跟踪仍然存在。

调查此输出可能会指出您的根本原因。

将logcat日志存储到文件的简单方法是使用adb logcat -d > logcat.txt命令

编辑:你还可以尝试另一件事。连接调试器时。

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED com.example.app将'com.example.app'更改为您的应用程序名称。这将向您的广播接收器发送BOOT_COMPLETE消息。

© www.soinside.com 2019 - 2024. All rights reserved.