![](/img/trans.png)
[英]IntentService's onDestroy method getting called straight after onHandleIntent
[英]Method onHandleIntent() does not get called
經過幾個小時的研究,我終於咨詢了官方的幫助。 為什么不調用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) {
}
可能是你的意圖服務沒有啟動,因為你正在重寫onStartCommand()
方法,因為android文檔說:
“你不應該為
(onStartCommand())
覆蓋這個方法(onStartCommand())
。而是覆蓋onHandleIntent(Intent)
,系統在IntentService收到啟動請求時調用它。”
希望這對你有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.