2013-04-02 25 views
6

Dopo molte ore di ricerca, sto finalmente consultando l'aiuto ufficiale. Perché non viene chiamato onHandleIntent()? C'è qualcosa di sbagliato qui?Metodo onHandleIntent() non viene chiamato

In attività principale onCreate():

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

Che iss esso. Il onStartCommand() viene chiamato, ma non 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(); 
    } 
} 

Questo è all'interno del 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

E come si sta chiamando IntentService? –

+0

Iniziale modificato :) – KickAss

risposta

11

Può essere il vostro servizio intento non si avvia perché stai facendo override onStartCommand() Metodo documentazione Android dice:

"Non si deve sovrascrivere questo metodo (onStartCommand()) per il proprio IntentService. Sostituire invece onHandleIntent(Intent), che il sistema chiama quando IntentService riceve una richiesta di avvio. "


spero che questo vi aiuterà

+0

Sì, l'ho risolto, ma ora ho un nuovo problema. Sto cambiando lo sfondo in background usando il codice in EDIT 1 sopra. Lo sfondo cambia come dovrebbe quando viene chiamato dal thread Attività principale, ma quando viene utilizzato all'interno del metodo onHandleIntent, lo sfondo cambia in un colore solido casuale. – KickAss

+0

Se intentService sta cambiando lo sfondo con un colore casuale casuale, il problema potrebbe riguardare la bitmap. Eseguire il debug e controllare se sta generando bitmap corretta o meno. –

+0

Ciao. Ho creato un nuovo post per questo problema con lo sfondo per tenerlo pulito, per favore controlla il codice :) http://stackoverflow.com/questions/15756253/onhandleintent-wallpaper-change-not-working-correctly – KickAss

Problemi correlati