6

After many hours of researching I am finally consulting official help. Why does not onHandleIntent() get called? Is there something wrong here?

In main activity onCreate():

mService = new Intent(context, xyz.class);
startService(mService);

That iss it. The onStartCommand() gets called, but not onHandleIntent()

package com.autoalbumwallaperplus;

import android.app.IntentService;
import android.content.Intent;
import android.widget.Toast;

public class xyz extends IntentService {
    public xyz() {
        super("bmp");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this,"onStartCommand works!", Toast.LENGTH_SHORT).show();
        return super.onStartCommand(intent,flags,startId);
    }

    @Override
    protected void onHandleIntent(Intent workIntent) {
        Toast.makeText(this,"onHandleIntent works!", Toast.LENGTH_SHORT).show();
    }
}

This is inside the OnHandleIntent

    String imagepath = workIntent.getStringExtra("String");
    Toast.makeText(this, "it works" , Toast.LENGTH_SHORT).show();
    DisplayMetrics displayMetrics = new DisplayMetrics();
    WindowManager hi = ((WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE));
    int height = displayMetrics.heightPixels;
    int width = displayMetrics.widthPixels << 2;

    // ... First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    Bitmap decodedSampleBitmap = BitmapFactory.decodeFile(imagepath, options);

    // ... Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, width, height);

    // ... Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    decodedSampleBitmap = BitmapFactory.decodeFile(imagepath, options);

    // ... Set Wallpaper
    //Context context = getApplicationContext();
    WallpaperManager wm = WallpaperManager.getInstance(this);

    try {
        wm.setBitmap(decodedSampleBitmap);
    } catch (IOException e) {
    }
Turing85
  • 18,217
  • 7
  • 33
  • 58
KickAss
  • 4,210
  • 8
  • 33
  • 41

1 Answers1

11

May be your intent service isn't starting because you are overriding onStartCommand() method as android documentation says:

"You should not override this method(onStartCommand()) for your IntentService. Instead, override onHandleIntent(Intent), which the system calls when the IntentService receives a start request."


Hope so this will help you

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
M.J
  • 586
  • 6
  • 16
  • Yes that fixed it, but now I have a new problem. I am changing the wallpaper in the background using the code in EDIT 1 above. The wallpaper changes as it should when called from the main Activity thread, but when used inside onHandleIntent method, the wallpaper changes to a random solid colour. – KickAss Apr 02 '13 at 04:23
  • If intentService is changing your wallpaper to some random solid colour then problem could be with the bitmap. Debug and check if it is generating right bitmap or not. – M.J Apr 02 '13 at 04:40
  • Hi. I made a new post for this wallpaper issue to keep it clean, please check the code :) http://stackoverflow.com/questions/15756253/onhandleintent-wallpaper-change-not-working-correctly – KickAss Apr 02 '13 at 04:42
  • How could that fix the issue when all his `onStartCommand()` method was effectively doing was calling `super.onStartCommand()`? Or was the Toast message somehow causing the problem? – ban-geoengineering Nov 05 '16 at 13:34