如何在设备重新启动时启动的广播接收器中获取配置值? (MAUI Android)

huangapple go评论83阅读模式
英文:

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&lt;bool&gt;(&quot;MyParameter&quot;, 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&lt;bool&gt;(&quot;Test&quot;, 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&lt;bool&gt;(&quot;Test&quot;, true);    
    }

The result image:

如何在设备重新启动时启动的广播接收器中获取配置值? (MAUI Android)

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.

如何在设备重新启动时启动的广播接收器中获取配置值? (MAUI Android)

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:

      &lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.RECEIVE_BOOT_COMPLETED&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.FOREGROUND_SERVICE&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.START_FOREGROUND_SERVICES_FROM_BACKGROUND&quot; /&gt;

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.

huangapple
  • 本文由 发表于 2023年5月15日 01:08:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248736.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定