java 方法 onHandleIntent() 没有被调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15755785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Method onHandleIntent() does not get called
提问by KickAss
After many hours of researching I am finally consulting official help. Why does not onHandleIntent()
get called? Is there something wrong here?
经过数小时的研究,我终于咨询了官方帮助。为什么没有onHandleIntent()
被调用?这里有什么问题吗?
In main activity onCreate()
:
在主要活动中onCreate()
:
mService = new Intent(context, xyz.class);
startService(mService);
That iss it. The onStartCommand()
gets called, but not onHandleIntent()
就是这样。在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();
}
}
This is inside the OnHandleIntent
这是在 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) {
}
回答by M.J
May be your intent service isn't starting because you are overriding onStartCommand()
method as android documentation says:
可能是您的意图服务未启动,因为您正在重写onStartCommand()
方法,如 android 文档所述:
"You should not override this method
(onStartCommand())
for your IntentService. Instead, overrideonHandleIntent(Intent)
, which the system calls when the IntentService receives a start request."
“你不应该
(onStartCommand())
为你的 IntentService重写这个方法。相反,重写onHandleIntent(Intent)
,当 IntentService 收到启动请求时系统调用它。”
Hope so this will help you
希望这会帮助你