Enable background services and Jobs (or FCM) in Chinese ROMs

Himanshu Singh
Apr 25, 2018 · 5 min read
Image for post
Image for post
Every Problem has a work around and if it works fine and suits your need then its kinda solution for you



Image for post
Image for post
Locked app in App’s tray
val intent = Intent()
val manufacturer = android.os.Build.MANUFACTURER
when(manufacturer) {
"xiaomi" ->
intent.component=
ComponentName("com.miui.securitycenter",
"com.miui.permcenter.autostart
.AutoStartManagementActivity")
"oppo" ->
intent.component =
ComponentName("com.coloros.safecenter",
"com.coloros.safecenter.permission.startup
.StartupAppListActivity")
"vivo" ->
intent.component =
ComponentName("com.vivo.permissionmanager",
"com.vivo.permissionmanager.activity
.BgStartUpManagerActivity")
}

val list = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
if (list.size > 0) {
context.startActivity(intent)
}

<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="com.test.intent.action.ALARM" />
</intent-filter>
</receiver>
class AlarmJobIntentService : JobIntentService() {

override fun onHandleWork(intent: Intent) {
/* your code here */
/* reset the alarm */
Util.showDebugLog("setAlarmCtx", "started Bottom")
AlarmReceiver.setAlarm(false)
stopSelf()
}

companion object {

/* Give the Job a Unique Id */
private val
JOB_ID = 1000

fun enqueueWork(ctx: Context, intent: Intent) {
JobIntentService.enqueueWork(ctx, AlarmReceiver::class.java, JOB_ID, intent)
}
}
}
class AlarmReceiver : BroadcastReceiver() {


override fun onReceive(context: Context, intent: Intent) {
/* enqueue the job */
AlarmJobIntentService.enqueueWork(context, intent)
}

companion object {

val CUSTOM_INTENT = "com.test.intent.action.ALARM"

val ctx = ApplicationClass.getAppContext()

fun cancelAlarm() {
val alarm = ctx.getSystemService(Context.ALARM_SERVICE)
as AlarmManager

/* cancel any pending alarm */
alarm.cancel(pendingIntent)
}

@RequiresApi(VERSION_CODES.M)
fun setAlarm(force: Boolean) {
cancelAlarm()
val alarm = ctx.getSystemService(Context.ALARM_SERVICE)
as AlarmManager
// EVERY N MINUTES
val
delay = (1000 * 60 * N).toLong()
var `when` = System.currentTimeMillis()
if (!force) {
`when` += delay
}

/* fire the broadcast */
val
SDK_INT = Build.VERSION.SDK_INT
when {
SDK_INT < Build.VERSION_CODES.KITKAT -> alarm.set(AlarmManager.RTC_WAKEUP, `when`, pendingIntent)
SDK_INT < Build.VERSION_CODES.M -> alarm.setExact(AlarmManager.RTC_WAKEUP, `when`, pendingIntent)
else -> alarm.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, `when`, pendingIntent)
}
}

private val pendingIntent: PendingIntent
get() {
val alarmIntent = Intent(ctx, AlarmManagerTaskBroadCastReceiver::class.java)
alarmIntent.action = CUSTOM_INTENT

return PendingIntent.getBroadcast(ctx, 0, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT)
}
}
}
AlarmReceiver.setAlarm(true); /* true will force the broadcast */
AlarmReceiver.cancelAlarm();

Alarm Scheduling Documentation

MindOrks

Our community publishes stories worth reading on Android…

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store