2013-04-02 133 views
6

經過許多小時的研究,我終於諮詢了官方的幫助。爲什麼不叫onHandleIntent()?這裏有什麼不對嗎?方法onHandleIntent()不會被調用

在主要活動onCreate()

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

這ISS它。該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) { 
    } 
+0

和你如何調用IntentService? –

+0

編輯開始帖子:) – KickAss

回答

11

裏面可能是你的意圖服務沒有啓動,因爲要覆蓋onStartCommand()方法Android文檔說:

」您不應該爲您的 IntentService覆蓋此方法(onStartCommand()),而應覆蓋onHandleIntent(Intent),其中 系統在IntentService收到啓動請求時調用。「


希望如此,這將幫助你

+0

是的,修復它,但現在我有一個新的問題。我正在使用上面的編輯1中的代碼更改背景中的壁紙。從主Activity活動線程調用時,壁紙會發生變化,但在onHandleIntent方法中使用時,壁紙會變爲隨機純色。 – KickAss

+0

如果intentService將您的壁紙更改爲某種隨機純色,則問題可能與位圖有關。調試並檢查它是否生成正確的位圖。 –

+0

嗨。我爲這個壁紙問題做了一個新的職位,以保持它的乾淨,請檢查代碼:) http://stackoverflow.com/questions/15756253/onhandleintent-wallpaper-change-not-working-correctly – KickAss