Android 设备启动时,我的广播接收器不工作
MyAndroid 设备启动时,我的广播接收器不工作,android,xamarin,broadcastreceiver,cross-platform,bootcompleted,Android,Xamarin,Broadcastreceiver,Cross Platform,Bootcompleted,MyAndroidManifest、Broadcastreceiver和服务类如下所示 DetectBootUp.cs: [BroadcastReceiver] [IntentFilter(new[] { Intent.ActionBootCompleted })] public class DetectBootUp : BroadcastReceiver { public override void OnReceive(Context context,
AndroidManifest
、Broadcastreceiver
和服务类如下所示
DetectBootUp.cs:
[BroadcastReceiver]
[IntentFilter(new[] { Intent.ActionBootCompleted })]
public class DetectBootUp : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Intent bootUp = new Intent(context, typeof(AndroidService));
context.StartService(bootUp);
}
}
AndroidService.cs
[Service]
public class AndroidService : Service
{
public override void OnCreate()
{
Toast.MakeText(this, "Service Created", ToastLength.Long).Show();
Log.Debug("BroadCastReceiverBoot", "OnCreate");
}
public override IBinder OnBind(Intent intent)
{
return null;
}
public override void OnDestroy()
{
Toast.MakeText(this, "Service Destroyed", ToastLength.Long).Show();
Log.Debug("BroadCastReceiverBoot", "onDestroy");
ApplicationContext.StartService(new Intent(ApplicationContext, typeof(AndroidService)));
}
[return: GeneratedEnum]
public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
{
Toast.MakeText(this, "Service Started", ToastLength.Long).Show();
Log.Debug("BroadCastReceiverBoot", "OnStart");
return StartCommandResult.Sticky;
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:label="NotificationExample">
<receiver
android:name=".DetectBootUp"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
<service android:name=".AndroidService"
android:enabled="true"
android:exported="false">
</service>
</application>
</manifest>
当设备重新启动时,我希望执行services类中的进程。但这会产生一个错误,为什么?
当我运行应用程序时,它工作正常,但我希望它在设备重新启动时自动执行。接收器可能有错误,但我找不到在哪里 在安卓3.1之后,“系统将标志\u排除\u停止\u包
添加到所有广播意图中。”这意味着,在3.1之后,所有应用程序在启动时停止。为什么?出于安全原因
有一些规则可以关闭标志标志\u排除\u停止\u包
(1) 您的应用程序需要手机存储,而不是SD卡,否则将设置标志<代码>启动完成在装入外部存储器之前发送。如果应用程序安装到外部存储器,它将不会收到BOOT\u COMPLETE
广播消息
(2) 如果用户按下“设置”中的“强制关闭”或“无响应应用”按钮,则会设置该标志
(3) 如果应用程序从未运行过,则会设置该标志(从不相对于当前引导状态;O)从不表示在该引导中,或者在上次引导状态中使该标志无效)
如果遵循规则,接收器将在启动时运行(未设置标志)。错误是什么?你有stacktrace吗?没有stacktrace。我不知道stacktrace:)安装应用程序时,我可以强制将应用程序保存在手机存储器中吗?如果这是可能的,这是如何做到的?我不理解“保存在手机内存中”,如果它在内存中(让我们称之为RAM,即使它不是),那么它不是“永久保存”,即不在存储中。很明显,Android操作系统跟踪的是标志,而不是应用程序。我可以这样解决这个问题吗?在AndroidManifest.xml中编写此代码。
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
StartService(new Android.Content.Intent(Application.Context, typeof(AndroidService)));
}
}