I have this broadcast receiver for ACTION_BOOT_COMPLETED
:
[BroadcastReceiver(Enabled = true, DirectBootAware = true, Exported = true)]
[IntentFilter(new[] { Intent.ActionBootCompleted }, Priority = (int)IntentFilterPriority.HighPriority)]
public class BootReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Intent startIntent = new Intent(context, typeof(PService));
startIntent.AddFlags(ActivityFlags.NewTask);
context.StartForegroundService(startIntent);
}
}
My MainActivity:
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
StartForegroundService(new Intent(this, typeof(PService)));
if (!Android.Provider.Settings.CanDrawOverlays(this))
{
Intent intent = new Intent(Android.Provider.Settings.ActionManageOverlayPermission);
StartActivityForResult(intent, 0);
}
}
My PService:
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
var seconds = TimeSpan.FromSeconds(15);
Device.StartTimer(seconds, () =>
{
Root r = new Root();
MySqlConnection con = new MySqlConnection("*PRIVATE DATA*");
con.Open();
MySqlCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT * FROM user_data";
MySqlDataReader reader = cmd.ExecuteReader();
DateTime user = DateTime.Now;
while (reader.Read())
{
if (reader.GetString(0) == Preferences.Get("GUID", "ERROR"))
{
user = DateTime.ParseExact(reader[1].ToString(), "HH:mm:ss", CultureInfo.InvariantCulture);
r.ID = reader.GetString(2);
}
}
async void LoadCallLog()
{
var Logg = DependencyService.Get<ICallLog>().GetCallLogs();
r.calls = (List<CallLogModel>)Logg;
var serialized = JsonConvert.SerializeObject(r);
using (var client = new HttpClient())
{
await client.PostAsync("https://webhook.site/dd7cd0f8-6bd8-4e9f-961d-6b105e1dc7eb", new StringContent(serialized));
}
}
if (user.ToString("HH:mm") == DateTime.Now.ToString("HH:mm"))
{
LoadCallLog();
Thread.Sleep(60000);
}
return true;
});
return StartCommandResult.Sticky;
}
But it doesn't launch my service once boot is completed. Manifest is automatically generated by xamarin. I know problem is that in new API (26+) are some restrictions.
Can someone help me with it what I should change so this code will work? I'm using 29 API version.