Xamarin android应用程序在使用BroadcastReceiver启动时崩溃,但启动了吗?
我正在尝试在Xamarin中启动一个应用程序构建,当推送到运行android 7.1 API 25的模拟器时,该应用程序可以正常运行。但是,当我想在启动时从应用程序启动服务时,应用程序会崩溃,但似乎仍在运行和工作?因为我无法在重新启动时调试模拟器,所以我不知道哪里出了问题 我有以下广播接收机:Xamarin android应用程序在使用BroadcastReceiver启动时崩溃,但启动了吗?,android,xamarin,crash,broadcastreceiver,boot,Android,Xamarin,Crash,Broadcastreceiver,Boot,我正在尝试在Xamarin中启动一个应用程序构建,当推送到运行android 7.1 API 25的模拟器时,该应用程序可以正常运行。但是,当我想在启动时从应用程序启动服务时,应用程序会崩溃,但似乎仍在运行和工作?因为我无法在重新启动时调试模拟器,所以我不知道哪里出了问题 我有以下广播接收机: [BroadcastReceiver] [IntentFilter(new[] { Intent.ActionBootCompleted })] public class BootBroadcastRec
[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>
有人知道它为什么会崩溃然后运行吗?任何提示或解决方案都将不胜感激 在模拟器启动后,可以从命令行运行adb logcat。Android会将错误缓存一段时间,所以任何崩溃报告和堆栈跟踪仍然存在
调查此输出可能会指出根本原因
将logcat日志存储到文件的一种简单方法是使用以下命令adb logcat-d>logcat.txt
编辑:
还有一件事你可以试试。当调试器被附加时
adb shell am广播-android.intent.action.BOOT_已完成com.example.app
将“com.example.app”更改为您的应用程序名称。这将向您的广播接收器发送BOOT_COMPLETE(启动完成)消息。如果启动后仍能正常工作,则没有任何用处,对吗?因为服务在模拟器启动时启动。不过,我会尝试一下。当我运行adb shell am broadcast-一个android.intent.action.BOOT_COMPLETED com.example.app时,它似乎说它没有权限。舱单不应该处理这件事吗?好吧,看来维杰是对的。最后一个命令告诉我,我没有足够的权限,如果API高于23,您自己需要明确请求权限,例如:将需要一段时间才能解决这个问题,但这似乎是下一步。很抱歉,发送BOOT_COMPLETE操作似乎需要adb以root身份运行。根据:井根似乎不能与调试一起工作。然后返回:-Broadcast completed:result=0这仍然不能解释它在启动时崩溃然后启动的原因。那么可能不是权限问题。。
[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);
}