Geofencing does not work because some devices only allow background services for any whitelist by default. If your application should also work this way, you must enable Autostart from the settings, the code below will help you enable Autostart for your application. If AutoPlay is enabled, your service will work well in the background.
For the phone to work better, some companies will stop all background services of the application ("Some whitelists will only have permission to execute services in the background, for example: WhatsApp, Google Apps and well-known applications."). So if our application should also work that way, we should enable Autostart Services.
Autostart:
When the Android system boots up, it sends a download completion event. Android applications can listen to and record this event to take certain actions, such as automatically starting an activity or service.
At the moment, it is impossible to determine whether Autostart is enabled or not. Thus, you can redirect them to the settings and tell the user to enable it. I provide redirection codes for most regular telephone companies that will kill background service. (Show it only to the user, control it using SharedPreference, as I said earlier there is no way to determine if it is enabled or disabled.)
To achieve this,
Declare permission in AndroidManifest.xml . Add the android.permission.RECEIVE_BOOT_COMPLETED permission to the application manifest file immediately before the node application declaration:
This is the function code to be called.
private void enableAutoStart() { if (Build.BRAND.equalsIgnoreCase("xiaomi")) { new MaterialDialog.Builder(MainActivity.this).title("Enable AutoStart") .content( "Please allow AppName to always run in the background,else our services can't be accessed.") .theme(Theme.LIGHT) .positiveText("ALLOW") .onPositive(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { Intent intent = new Intent(); intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity")); startActivity(intent); } }) .show(); } else if (Build.BRAND.equalsIgnoreCase("Letv")) { new MaterialDialog.Builder(MainActivity.this).title("Enable AutoStart") .content( "Please allow AppName to always run in the background,else our services can't be accessed.") .theme(Theme.LIGHT) .positiveText("ALLOW") .onPositive(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { Intent intent = new Intent(); intent.setComponent(new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity")); startActivity(intent); } }) .show(); } else if (Build.BRAND.equalsIgnoreCase("Honor")) { new MaterialDialog.Builder(MainActivity.this).title("Enable AutoStart") .content( "Please allow AppName to always run in the background,else our services can't be accessed.") .theme(Theme.LIGHT) .positiveText("ALLOW") .onPositive(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { Intent intent = new Intent(); intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity")); startActivity(intent); } }) .show(); } else if (Build.MANUFACTURER.equalsIgnoreCase("oppo")) { new MaterialDialog.Builder(MainActivity.this).title("Enable AutoStart") .content( "Please allow AppName to always run in the background,else our services can't be accessed.") .theme(Theme.LIGHT) .positiveText("ALLOW") .onPositive(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { try { Intent intent = new Intent(); intent.setClassName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity"); startActivity(intent); } catch (Exception e) { try { Intent intent = new Intent(); intent.setClassName("com.oppo.safe", "com.oppo.safe.permission.startup.StartupAppListActivity"); startActivity(intent); } catch (Exception ex) { try { Intent intent = new Intent(); intent.setClassName("com.coloros.safecenter", "com.coloros.safecenter.startupapp.StartupAppListActivity"); startActivity(intent); } catch (Exception exx) { } } } } }) .show(); } else if (Build.MANUFACTURER.contains("vivo")) { new MaterialDialog.Builder(MainActivity.this).title("Enable AutoStart") .content( "Please allow AppName to always run in the background.Our app runs in background else our services can't be accesed.") .theme(Theme.LIGHT) .positiveText("ALLOW") .onPositive(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { try { Intent intent = new Intent(); intent.setComponent(new ComponentName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity")); startActivity(intent); } catch (Exception e) { try { Intent intent = new Intent(); intent.setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity")); startActivity(intent); } catch (Exception ex) { try { Intent intent = new Intent(); intent.setClassName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.BgStartUpManager"); startActivity(intent); } catch (Exception exx) { ex.printStackTrace(); } } } } }) .show(); } }