JobIntentService заканчивается преждевременно
После орео мой сервис заканчивается преждевременно. Моя цель - это исправить. Из основной деятельности я бегу
Intent mServiceIntent = new Intent();
mServiceIntent.putExtra(....)
...
HintergrundDienst.enqueueWork(getBaseContext(),HintergrundDienst.class,JI,mServiceIntent);
HintergrundDienst
public class HintergrundDienst extends JobIntentService {
public static volatile boolean shouldContinue = true;
public static volatile boolean rauschen = false;
public static volatile boolean laeuft = true;
static final int JOB_ID = 1000;
static final int ONGOING_NOTIFICATION_ID = 33;
PowerManager.WakeLock wakeLock;
static void enqueueWork(Context context, Intent work) {
enqueueWork(context, HintergrundDienst.class, JOB_ID, work);
}
Notification notification;
@RequiresApi(api = Build.VERSION_CODES.O)
void vordergrund(String Titel, String Text)
{
Intent notificationIntent = new Intent(this, HintergrundDienst.class);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(Titel,
Text,
NotificationManager.IMPORTANCE_DEFAULT);
channel.setSound(null,null);
mNotificationManager.createNotificationChannel(channel);
}
notification =
new Notification.Builder(this,Titel)
.setContentTitle(Titel)
.setContentText(Text)
.setSmallIcon(R.drawable.kleinesicon)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentIntent(pendingIntent)
.setTicker("setTicker")
.build();
startForeground(ONGOING_NOTIFICATION_ID, notification);
}
@Override
protected void onHandleWork(Intent intent) {
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakelockTag");
wakeLock.acquire();
vordergrund(intent.getStringExtra("test"),"Titel");
//Some long task
}
@Override
public void onDestroy() {
super.onDestroy();
stopForeground(true);
wakeLock.release();
}
}
Теперь на Дестрой в HintergrundDienst
работает без //Some long task
закончив. Похоже, что таймер работает, и независимо от того, как работает wakelock, служба прерывается.
Как я могу предотвратить это преждевременно?
Я старался startForegroundService
в основной деятельности, но он хочет только намерение. Как узнать, какую службу запустить, если я не могу пройти HintergrundDienst
или же HintergrundDienst.class
к этому?