Same as the title.
I want to use sendBroadcast(intent)
to start broadcast;
and then in the broadcast.java
use the onReceive()
, like so:
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "successful", Toast.LENGTH_LONG).show();
MediaPlayer.create(context, R.raw.canon).start();
}
But VM can not do it, and it says: the media unable to create....
I'm a beginner, I don't know how to deal with it.
Please help me, thank you :-)
No, you are not supposed to start anything asynchronous or long running in onReceive
. If you need to run something in background, then use Service
.
That's because as soon as onReceive
returns, the component is considered dead. So no background operations are allowed to be tied to this component. And even more, Android can kill process as soon as onReceive
returns if this was the only component in process.