Skip to content

Instantly share code, notes, and snippets.

@hi-manshu
Created October 16, 2018 12:33
Show Gist options
  • Save hi-manshu/1d6c63f40fad65f5c9bbce89a624f86f to your computer and use it in GitHub Desktop.
Save hi-manshu/1d6c63f40fad65f5c9bbce89a624f86f to your computer and use it in GitHub Desktop.
// CALLING SERVICE FROM MAINACTIYITY:
Intent service = new Intent(SettingsActivity.this, ScreenService.class);
//Changes - 1
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
//Changes -2
MainActivity.this.startForegroundService(mContext, service);
} else {
startService(service);
}
//ScreenService.java:
onCreate() {
''
//Run only here and not on onStartCommand
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
displayForeground()
}
..
..
}
onStartCommand() {
''
// displayForeground Notification
return START_STICKY;
..
..
}
displayFroeground () {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(NOTIFICATION_ID, new Notification.Builder(this).build());
}
//if not Oreo and above do this.
......
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment