6

经过数小时的研究,我终于咨询了官方帮助。为什么不onHandleIntent()被调用?这里有什么问题吗?

在主要活动中onCreate()

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

就是这样。被onStartCommand()调用,但没有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();
    }
}

这是在 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) {
    }
4

1 回答 1

11

可能是您的意图服务没有启动,因为您正在重写onStartCommand()方法,如 android 文档所述:

“你不应该为你的 IntentService 覆盖这个方法(onStartCommand())。而是覆盖onHandleIntent(Intent),当 IntentService 收到一个启动请求时系统调用它。”


希望这会对你有所帮助

于 2013-04-02T04:16:26.517 回答