gpt4 book ai didi

java - 方法 onHandleIntent() 没有被调用

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:37:52 27 4
gpt4 key购买 nike

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

在主要 Activity 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) {
}

最佳答案

可能是您的 Intent 服务未启动,因为您正在覆盖 onStartCommand() 方法,如 android 文档所述:

"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."


希望这对你有帮助

关于java - 方法 onHandleIntent() 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15755785/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com