Reputation: 31
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;
}
}
@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());
}
}
Upvotes: 0
Views: 2283
Reputation: 31
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
Upvotes: 1