【问题标题】:Xamarin android app crashes on boot with BroadcastReceiver but starts?Xamarin android 应用程序在使用 BroadcastReceiver 启动时崩溃但启动?
【发布时间】:2018-01-03 11:02:47
【问题描述】:

我正在尝试在 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】:

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

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

    将 logcat 日志存储到文件的一种简单方法是这个命令adb logcat -d &gt; logcat.txt

    编辑: 您还可以尝试另一件事。附加调试器时。

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

    【讨论】:

    • 如果这在它启动后有效,它没有任何用处吗?因为服务在模拟器启动时启动。不过会尝试一下。
    • 似乎当我运行 adb shell am broadcast -a android.intent.action.BOOT_COMPLETED com.example.app 它说它没有权限。清单不应该处理这个吗?
    • 好吧,看来 Vergeer 是对的。最后一个命令显示我没有足够的权限,并且使用更高的 API 然后 23 您自己明确需要请求权限,例如:blog.xamarin.com/… 这需要一段时间才能弄清楚,但这似乎是下一步。
    • 很抱歉,发送 BOOT_COMPLETE 操作似乎需要 adb 以 root 身份运行。根据:stackoverflow.com/questions/40698450/…
    • 井根似乎不适用于调试。然后它返回: - 广播完成:结果 =0 这仍然没有解释为什么它在启动时崩溃然后启动。那么可能不是权限问题..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多