Я реализовал startForeground()
метод для запуска текущего уведомления, которое вызывается в onCreate()
. Есть два использования в двух разных классах. Я тестирую на API 27 (Android 8.1)
Первое использование (PictureCapturingService.java):
OnCreate ()
@Override
public void onCreate() {
super.onCreate();
Context mContext = this.getApplicationContext();
context = this.getApplicationContext();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startMyOwnForeground();
} else {
startForeground(1234, new Builder(getApplicationContext()).setContentTitle("Capturing Image").setContentText("").setSmallIcon(R.drawable.icon_notif).build());
}
startMyOwnForeground ()
@RequiresApi(api = Build.VERSION_CODES.O)
private void startMyOwnForeground() {
String channelID = "com.example.code";
NotificationChannel notificationChannel = new NotificationChannel(channelID, "Background Service", NotificationManager.IMPORTANCE_NONE);
notificationChannel.enableLights(false);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(notificationChannel);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelID);
Notification notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.icon_notif)
.setContentTitle("Capturing Image")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(1234, notification);
}
Второе использование (VideoRecordService.java):
Получает вызов от onCreate()
и startRecord()
OnCreate () :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startMyOwnForeground("Sending Message");
}
else {
startForeground(1234, new Builder(this).setContentTitle("Sending Message").setContentText("").setSmallIcon(R.drawable.icon_notif).build());
}
startRecord ()
private void startRecord() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startMyOwnForeground("Video Recording");
} else {
startForeground(1234, new Builder(getApplicationContext()).setContentTitle("Video Recording").setContentText("").setSmallIcon(R.drawable.icon_notif).build());
}
startMyOwnForeground
@RequiresApi(api = Build.VERSION_CODES.O)
private void startMyOwnForeground(String str) {
String channelID = "com.example.code";
NotificationChannel notificationChannel = new NotificationChannel(channelID, "Background Service", NotificationManager.IMPORTANCE_NONE);
notificationChannel.enableLights(false);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(notificationChannel);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelID);
Notification notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.icon_notif)
.setContentTitle(str)
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(1234, notification);
}
Я получаю следующую ошибку:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.raudram.barathikannamma, PID: 18871
android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6651)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)