Android 应用程序无法在启动时启动,使用 Android Xamarin 完成
我试图让我的应用程序在设备重新启动时启动,因此我创建了一个名为 BootReceiver
的类,该类从 BroadcastReceiver
扩展,并使用注册到 Android.intent 的意图过滤器.action.BOOT_COMPLETED 常量。
我在 Oncreate()...
中创建了 BootReceiver
类的实例,并将其注册到上面指定的过滤器中。我还在清单文件上声明了接收启动完成意图的权限,并以编程方式请求它。即使完成所有这些操作,重新启动设备时我的应用程序也不会启动。
BootReceiver 类
[BroadcastReceiver(Label ="BootReceiver", Enabled = true, Exported = true)]
[IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
class BootReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
// Launch our activity
if (intent.Action == "android.intent.action.BOOT_COMPLETED")
{
/*
Intent new_intent = new Intent(context, typeof(MainActivity));
new_intent.SetFlags(ActivityFlags.NewTask);
context.StartActivity(new_intent);
*/
Toast.MakeText(context, "trying to start Niskize app", ToastLength.Long).Show();
}
}
}
MainActivity 类
[Activity(Label = "@string/app_name", Theme = "@style/AppThemeWhite", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
BootReceiver bootReceiver;
protected override void OnCreate(Bundle savedInstanceState)
{
// Request for boot completed permissions
boot_permissions();
bootReceiver = new BootReceiver();
RegisterReceiver(bootReceiver, new IntentFilter("android.intent.action.BOOT_COMPLETED"));
}
}
如何使用 Android Xamarin 正确实现此功能?
I am trying to make my app start when my device reboots, so I created a class called BootReceiver
extending from BroadcastReceiver
with an intent filter registered with the Android.intent.action.BOOT_COMPLETED
constant.
I created an instance of the BootReceiver
class in my Oncreate()...
and registered it with the filter specified above. I also declared the permission for receiving boot completed intents on my manifest file and requested for it programmatically. Even after doing all that, my app does not start when restart my device.
BootReceiver class
[BroadcastReceiver(Label ="BootReceiver", Enabled = true, Exported = true)]
[IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
class BootReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
// Launch our activity
if (intent.Action == "android.intent.action.BOOT_COMPLETED")
{
/*
Intent new_intent = new Intent(context, typeof(MainActivity));
new_intent.SetFlags(ActivityFlags.NewTask);
context.StartActivity(new_intent);
*/
Toast.MakeText(context, "trying to start Niskize app", ToastLength.Long).Show();
}
}
}
MainActivity class
[Activity(Label = "@string/app_name", Theme = "@style/AppThemeWhite", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
BootReceiver bootReceiver;
protected override void OnCreate(Bundle savedInstanceState)
{
// Request for boot completed permissions
boot_permissions();
bootReceiver = new BootReceiver();
RegisterReceiver(bootReceiver, new IntentFilter("android.intent.action.BOOT_COMPLETED"));
}
}
How can I implement this correctly with Android Xamarin?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在不同版本的 Android 上尝试过你的代码。如果设备运行Android 9.0或更低版本,则该活动将成功启动。但在Android 10.0以上系统会拦截activity后台的启动。
更多信息:https://developer.android.com/guide/components/activities /background-starts
如果你在设备启动时立即启动应用程序,你会发现 Activity 会闪烁一次,这是因为 Activity 是前台的,不会被系统拦截。
我还尝试通过 AndroidManifest.xml 注册 BootReceiver,在 Android 12 上,您需要将 android:exported="true" 添加到接收器。
AndroidManifest.xml:
BootReceiver.cs
我将 Toast 放在第一行,以确保 bootreceiver 正常工作,而只是 startactivity 失败。
另外,在Android 10及更高版本上,似乎如果开发者希望应用程序在设备启动时自动启动,则需要将应用程序添加到白名单中。
I had tried your code on different versions of Android. If the device is with Android 9.0 or lower, the activity will start successfully. But on the Android 10.0 or higher, the system will intercept the starting of the activity background.
More information: https://developer.android.com/guide/components/activities/background-starts
And if you start the app at once when the device is on boot, you can seem the activity will flash once, this is because the activity is foreground and will not be intercept by the system.
I also tried to register the BootReceiver by the AndroidManifest.xml and on the Android 12 you need to add the android:exported="true" to the receiver.
AndroidManifest.xml:
BootReceiver.cs
I put the Toast on the first line to ensure the bootreceiver works correctly and just the startactivity failed.
In addition, on the Android 10 and higher, it seems that if the developer wants the app auto start when the device is on boot, need to add the app to the white list.