我正在尝试使用 Intent 服务运行背景音乐。OnhandleIntent 不会被调用。我尝试添加断点甚至代码 android.os.Debug.waitForDebugger(); 但我无法进入 OnHandleIntent。我已将服务添加到清单中(android:name=".BackgroundMusic"/>),但我不确定我在这里缺少什么。任何帮助,将不胜感激。
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());
}
}