入队工作导致奥利奥问题

时间:2018-08-08 12:43:55

标签: android android-8.0-oreo background-service

我有一个myactivity.java类,它像往常一样扩展了AppCompatActivity。我有一个名为HintergrundDienst的前台服务,该服务正在使用HintergrundDienst.enqueueWork(getBaseContext(),HintergrundDienst.class,JI,mServiceIntent);中的myactivity.java执行我要排队的内容。在奥利奥之前,这很好。但是现在,onDestroy中的HintergrundDienstenqueueWork添加的任务之间被随机调用,然后以一种不可预测的方式继续/重新启动队列。 奥利奥(Oreo)中有什么新原因导致这种情况?我该如何解决?

HintergrundDienst

package de.test

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.AudioFocusRequest;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.os.Build;
import android.os.Handler;
import android.os.PowerManager;
import android.provider.MediaStore;
import android.support.annotation.MainThread;
import android.support.annotation.RequiresApi;
import android.support.v4.app.JobIntentService;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

import java.util.Random;

public class HintergrundDienst extends JobIntentService {
    public static volatile boolean shouldContinue = true;
    static final int JOB_ID = 1000;
    static final int ONGOING_NOTIFICATION_ID = 33;

    /**
     * Convenience method for enqueuing work in to this service.
     */
    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);
    }
    PowerManager.WakeLock wakeLock;
    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onHandleWork(Intent intent) {
        PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                "MyWakelockTag");
        wakeLock.acquire();
        int WHMin = intent.getIntExtra("WHMin",0);
        ...
            vordergrund(intent.getStringExtra("ttt"),"fdgfdg");  
            ...
            nichtstun(200); 
        ...
            vordergrund(intent.getStringExtra("yxsc"),"sdfsdf");

    }

    void nichtstun(double dauer)
    {
        try {
            Thread.sleep((int)(dauer*1000));
        } catch (InterruptedException e) {
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"Ondestroy");
        stopForeground(true);
        wakeLock.release();
    }

    final Handler mHandler = new Handler();

    // Helper for showing toasts
    void toast(final CharSequence text) {
        mHandler.post(new Runnable() {
            @Override public void run() {
                Toast.makeText(HintergrundDienst.this, text, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

0 个答案:

没有答案