Created
October 16, 2018 12:33
-
-
Save hi-manshu/1d6c63f40fad65f5c9bbce89a624f86f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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