Skip to content

Instantly share code, notes, and snippets.

@GransonO
Last active December 15, 2023 07:48
Show Gist options
  • Save GransonO/8fd3bfa0521c914597e46ebaf3ae39d3 to your computer and use it in GitHub Desktop.
Save GransonO/8fd3bfa0521c914597e46ebaf3ae39d3 to your computer and use it in GitHub Desktop.
Autostart implementation for different Android OEMs
Working on Android phones can be quite interesting.
Recently I've had opportunities that required me to work on more advanced features of Android OS apart from the more common create app featurs that rely on pre-created with specified and well supported libraries e.g Jetpack Compose.
A common problem that many will face as they work on the Android phones, is starting an service on boot complete.
I will skip the process of implementing the broadcast receiver class and the manifest declaration.
Main point for this gist is to provide my finding based on the research I have done amd make another developers life easier, I mean, we stand on the shoulders of those that came before us.
Auto-Starting apps is not very easy for most developers, further, most OEMs make it quite difficult to find the setting manually. Asking the user to navigate to settings and do the work for you is quite a douting task.
Below, I have documented the function that can be used to open the Autostart activity and let the user allow your app to start on boot up.
You are welcome
private fun addAutoStartup() {
val manufacturer = Build.MANUFACTURER
baseLogger("The manufacturer ", manufacturer)
try {
val intent = Intent()
when {
manufacturer.contains("itel", ignoreCase = true) -> {
intent.component = ComponentName("com.transsion.phonemaster", "com.cyin.himgr.autostart.AutoStartActivity")
startActivityForResult(intent, PHONE_CODE)
}
manufacturer.contains("tecno", ignoreCase = true) -> {
intent.component = ComponentName("com.transsion.phonemaster", "com.cyin.himgr.autostart.AutoStartActivity")
startActivityForResult(intent, PHONE_CODE)
}
manufacturer.contains("infinix", ignoreCase = true) -> {
intent.component = ComponentName("com.transsion.phonemaster", "com.cyin.himgr.autostart.AutoStartActivity")
startActivityForResult(intent, PHONE_CODE)
}
manufacturer.contains("xiaomi", ignoreCase = true) -> {
intent.component = ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity")
startActivityForResult(intent, PHONE_CODE)
}
manufacturer.contains("oppo", ignoreCase = true) -> {
intent.setClassName("com.coloros.oppoguardelf", "com.coloros.powermanager.fuelgaue.PowerConsumptionActivity")
intent.component = ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity")
startActivityForResult(intent, PHONE_CODE)
}
manufacturer.contains("vivo", ignoreCase = true) -> {
intent.component = ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity")
startActivityForResult(intent, PHONE_CODE)
}
manufacturer.contains("Letv", ignoreCase = true) -> {
intent.component = ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity")
startActivityForResult(intent, PHONE_CODE)
}
manufacturer.contains("Honor", ignoreCase = true) -> {
intent.component = ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity")
startActivityForResult(intent, PHONE_CODE)
}
else -> {
baseLogger(
"Auto Loader Config",
"No configuration for this manufacturer"
)
}
}
} catch (e: Exception) {
baseLogger("Auto Load Exception", e.toString())
}
}
This function should open the Autostart Activity for different OEMs.
@OptIn(InternalCoroutinesApi::class)
@Deprecated("Deprecated in Java")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when(requestCode){
PHONE_CODE -> {
// Save result data for display
Save some persistant variable here that you can use to avoid calling the request function again when the user reopens the app, a shared preference entry would be sufficient.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment