Xerxes Linde
Lurker
After oreo, my service ends prematurely. My goal is to fix that. From the main activity, I run
HintergrundDienst
Now, onDestroy in `HintergrundDienst` is run without `//Some long task` having finished. It seems like a timer is running and no matter the wakelock, the service is being terminated.
How do I prevent this from happening prematurely?
I tried `startForegroundService` in the main activity, but it only wants an intent. How does it know which service to start, if I cannot pass `HintergrundDienst` or `HintergrundDienst.class` to it?
Code:
Intent mServiceIntent = new Intent();
mServiceIntent.putExtra(....)
...
HintergrundDienst.enqueueWork(getBaseContext(),HintergrundDienst.class,JI,mServiceIntent);
HintergrundDienst
Code:
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);
}
[USER=1021285]@override[/USER]
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
}
[USER=1021285]@override[/USER]
public void onDestroy() {
super.onDestroy();
stopForeground(true);
wakeLock.release();
}
}
Now, onDestroy in `HintergrundDienst` is run without `//Some long task` having finished. It seems like a timer is running and no matter the wakelock, the service is being terminated.
How do I prevent this from happening prematurely?
I tried `startForegroundService` in the main activity, but it only wants an intent. How does it know which service to start, if I cannot pass `HintergrundDienst` or `HintergrundDienst.class` to it?