2013-04-02 130 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