英文:
How could I get a configuration value in a BroadcastReceiver that starts when the device reboots? (MAUI Android)
问题
I have a BroadcastReceiver that it will start a foreground service when the device reboots.
This is the code:
[BroadcastReceiver(Enabled = true, Exported = true)]
[IntentFilter(new[] { Intent.ActionBootCompleted })]
public class ActionBootCompletedBroadcastReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
if (intent.Action == Intent.ActionBootCompleted)
{
ServicioConfiguracion miServicioConfiguracion = new ServicioConfiguracion();
bool miBlMyParameter = miServicioConfiguracion.GetParameter();
if (miBlMyParameter == true)
{
var foreGroundServiceIntent = new Intent(Android.App.Application.Context, typeof(MyForegroundService));
Android.App.Application.Context.StartForegroundService(intent);
context.StartForegroundService(foreGroundServiceIntent);
}
}
}
}
And this is the code of the configuration manager that reads the value of configuration of the application:
public bool GetParameter()
{
return Preferences.Default.Get<bool>("MyParameter", false);
}
The problem is that it seems that the BroadCastReceiver can't get the value of the parameter because if I comment the if that determines if to start the foreground service or not, it starts.
The other problem that I have is I don't know how to debug the BroadcastReceiver because when the device reboots, the debugger stops.
My idea is that the user can configure in the application if starts the foreground service or not when the device starts.
How could I get the values of configuration of the application in the BroadCastReceiver?
英文:
I have a BroadcastReceiver that it will start a foreground service when the device reboots.
This is the code:
[BroadcastReceiver(Enabled = true, Exported = true)]
[IntentFilter(new[] { Intent.ActionBootCompleted })]
public class ActionBootCompletedBroadcastReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
if (intent.Action == Intent.ActionBootCompleted)
{
ServicioConfiguracion miServicioConfiguracion = new ServicioConfiguracion();
bool miBlMyParamter = miServicioConfiguracion.GetParameter();
if (miBlMyParameter == true)
{
var foreGroundServiceIntent = new Intent(Android.App.Application.Context, typeof(MyForegroundService));
Android.App.Application.Context.StartForegroundService(intent);
context.StartForegroundService(foreGroundServiceIntent);
}
}
}
}
And this is the code of the configuration manager that reads the value of configuration of the application:
public bool GetParameter()
{
return Preferences.Default.Get<bool>("MyParameter", false);
}
The problem is that it seems that the BroadCastReceiver can't get the value of the parameter, because if I comment the if that determinates if start the foreground service or not, it starts.
The other problem that I have it is I don't know how to debug the BroadcastReceiver, because when the device reboots, the debugger stops.
My idea is that the user can configure in the application if starts the foreground service or not when the device starts.
How could I get the values of configuration of the application in the BroadCastReceiver?
Thanks.
答案1
得分: 1
I used the Toast to show the result:
[BroadcastReceiver(Exported = true)]
[IntentFilter(new[] { Intent.ActionBootCompleted })]
public class MyReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
var a = Preferences.Default.Get<bool>("Test", false);
Toast.MakeText(context, a.ToString(), ToastLength.Long).Show();
}
}
And set the value in the MainActivity:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Preferences.Default.Set<bool>("Test", true);
}
英文:
I used the Toast to show the result:
[BroadcastReceiver(Exported = true)]
[IntentFilter(new[] {Intent.ActionBootCompleted})]
public class MyReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
var a = Preferences.Default.Get<bool>("Test", false);
Toast.MakeText(context, a.ToString(), ToastLength.Long).Show();
}
}
And set the value in the MainActivity:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Preferences.Default.Set<bool>("Test", true);
}
The result image:
Update1:
I used the adb in the visual studio successfully. First of all, click Tools->Android->Android Adb Command Prompt to open the adb command panel.
And then use the adb root
command to get into root mode. After this, I can send the boot completed broadcast.
In addition, if you want to start foreground service in the background(after the device boot completed), you can add the following permission in the AndroidManifest.xaml:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.START_FOREGROUND_SERVICES_FROM_BACKGROUND" />
I can start the foreground service in the OnReceive method when I restarted the emulator android api 33.
And I checked your code in the OnReceive method: Android.App.Application.Context.StartForegroundService(intent);
this line should be deleted. The intent
is the parameter of the OnReceive method. Your foreground service intent is foreGroundServiceIntent
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论