简体   繁体   中英

IntentService OnHandleIntent is not being called

I am trying to run a background music using Intent service. The OnhandleIntent does not get called. I tried adding breakpoints and even the code android.os.Debug.waitForDebugger(); but i am unable to step into OnHandleIntent. I have added the service to the manifest( android:name=".BackgroundMusic"/>) and i am not sure what i am missing here. Any help would be appreciated.

public class BackgroundMusic extends IntentService
 {
   MediaPlayer mp;
   Uri uri;

public BackgroundMusic()
{
    super("BackgroundMusic");
     setIntentRedelivery(true);
}

@Override
protected void onHandleIntent(Intent intent)
{   
    try     
    {   
         android.os.Debug.waitForDebugger();
        int id=intent.getExtras().getInt("musicid");            
        mp=MediaPlayer.create(this,id);
        mp.prepare();
        mp.start();     
    }   
    catch (Exception e)
    {
        Log.e("Error",e.getMessage());
    }       
}

@Override
public void onCreate()
{
    super.onCreate();
}

@Override
public void onDestroy() 
{
    super.onDestroy();
    if (mp != null) 
        mp.release();
        mp=null;
}

}

Activity Code

    @Override
public void onCreate(Bundle savedInstanceState)
{
    try
    {
            super.onCreate(savedInstanceState);
             LMain=new LinearLayout(this);  
                     LMain.setOrientation(LinearLayout.HORIZONTAL);
           LayoutParams pFill=new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
              LMain.setLayoutParams(pFill);
            imageview=new ImageView(this);          
                            imageview.setLayoutParams(pFill);
            setContentView(LMain);

            Intent IntImage=getIntent();
            int id=IntImage.getExtras().getInt("id");
                mresourceid=new ImageAdapter(this).music[id];       
            imageview.setImageResource(new ImageAdapter(this).images[id]);
            Intent IntMusic=new Intent(ImageDetail.this,BackgroundMusic.class);
            IntMusic.putExtra("musicid", mresourceid);
            startService(IntMusic);     
    }
    catch(Exception e)
    {
        Log.e("error",e.getMessage());
    }

}

It works. I was earlier running the code on device with OS Version 2.3.4 . Intent Services are supported from API level 3. Wanted to update just in case if anyone is doing the same what i did

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM