2

我想将任务添加到JobIntentService queue,目前我已经创建JobIntentService,这对于一项任务来说是正确的,但是当我enqueueWork在完成之前打电话时existing service,它不起作用。

下面是我的JobIntentService

public class SaveImageService extends JobIntentService {

    private String TAG = "SaveImageService";
    private int textureViewDefaultHeight =1100;
    static final int JOB_ID = 1101;
    private static Context mContext;
    private ResultReceiver mResultReceiver;
    public static final int SHOW_RESULT = 57;
    public static final String RECEIVERstr = "receiver";

    public static void enqueueWork(Context context, Intent work, SaveImageReceiver saveImageReceiver) {
        mContext = context;
        Intent intent = work;
        intent.putExtra(RECEIVERstr, saveImageReceiver);
        enqueueWork(context, SaveImageService.class, JOB_ID, intent);
   }

    @Override
    protected void onHandleWork(@NonNull Intent intent) {
        Log.e(TAG, "** onHandleWork");

    if (intent.hasExtra("fileUri")) {

        mResultReceiver = intent.getParcelableExtra(RECEIVERstr);
        Log.e(TAG, "** onHandleWork if has extra");
        Uri oldUri = intent.getParcelableExtra("fileUri");
        int textureViewHeight = intent.getIntExtra("textureViewHeight",textureViewDefaultHeight);
        Uri tempUri = getBitmapForBatch(oldUri,textureViewHeight);

        if(tempUri!=null){
            Log.e(TAG, "** if(tempUri!=null){");
            Bundle bundle = new Bundle();
            bundle.putParcelable("ResultUri", tempUri);
            if (mResultReceiver != null) {
                Log.e(TAG, "** onHandleWork mResultReceiver not null");
                mResultReceiver.send(SHOW_RESULT, bundle);
            } else
                Log.e(TAG, "** onHandleWork mResultReceiver isss null");
        }else
            Log.e(TAG, "** else (tempUri!=null){");
    }else{
        Log.e(TAG, "** onHandleWork else uri not get");
    }
}
 }

每次用户点击照片时我都会启动它

Intent mIntent = new Intent(context, SaveImageService.class);
                            mIntent.putExtra("fileUri", fileUri);
                            mIntent.putExtra("textureViewHeight", textureView.getHeight());
                            SaveImageService.enqueueWork(context, mIntent, mSaveImageReceiver);

提前感谢您的帮助。

4

0 回答 0