Hi,
Problems:
And the broadcastreceiver has the following structure:
[BroadcastReceiver(Enabled = true, Exported = true, DirectBootAware = true)] [IntentFilter(new[] { Intent.ActionBootCompleted, Intent.ActionLockedBootCompleted }, Priority = (int)IntentFilterPriority.HighPriority)] public class BootReceiver : BroadcastReceiver { public override void OnReceive(Context context, Intent intent) { CustomHelper.StartService(true); // This call a service with: Application.Context.StartForegroundService (new Intent (intent)); } }
And I use Android.Manifest.Permission.ForegroundService.
I have read about the limitations in the documentation but maybe I do not apply it well.
**Can someone show me a complete example code? Thanks!
**
https://developer.android.com/about/versions/oreo/background.html#broadcasts
https://developer.android.com/topic/performance/background-optimization.html
Answers
ForegroundService will not work when the app in background.
Could you post the related code?
@ephramd Use JobIntenService for Android OS Oreo and above to run the application in background and foreground.
https://forums.xamarin.com/discussion/134197/any-xamarin-android-c-sample-for-jobintentservice
And also add Broadcast Receiver Intent Filter dynamically.
@yelinzh @Anandmaurya
thanks for answering and orget what I mentioned.
I just want a BroadcastReceiver that after turning on the device (or removing the power cable) start a service without using a fixed notification (as foreground does) and that works on Android 9.0.
Can anyone share a simple example code with the structure code (Manifest.xml, Main.cs, Service.cs and BroadcastReceiver.cs)?
I am reading the documentation but I do not see any full example, I have nothing clear how to make it work in the current version of android.
@yelinzh @Anandmaurya I tried this:
MainActivity.cs:
protected override void OnCreate(Bundle savedInstanceState) {
base.OnCreate(savedInstanceState);
AndroidManifest.xml
RegistrationIntentService:
[Service(Name = "com.MyPackageName.RegistrationIntentService", Permission = "android.permission.BIND_JOB_SERVICE", Exported = true)]
public class RegistrationIntentService : Android.Support.V4.App.JobIntentService {
CustomBroadcastReceiver:
[BroadcastReceiver(Enabled = true, Exported = true, DirectBootAware = true)]
class CustomBroadcastReceiver : BroadcastReceiver {
public override void OnReceive(Context context, Intent intent) {
Console.WriteLine("------------------ MiBroadcastReceiver");
Log.Info("Fran", "------------------ MiBroadcastReceiver");
Toast.MakeText(Application.Context, "MiBroadcastReceiver", ToastLength.Short).Show();