JobIntentService提前结束

时间:2018-08-09 22:11:44

标签: android android-8.0-oreo 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中的onDestroy。似乎计时器正在运行,无论唤醒锁如何,服务都将终止。

如何防止这种情况过早发生?

我在主要活动中尝试了startForegroundService,但它只想要一个意图。如果我无法将HintergrundDienstHintergrundDienst.class传递给服务,它如何知道要启动哪个服务?

0 个答案:

没有答案