The way I say this is to use the service in Android to turn on, that is, when you go in and play out, exit and stop.
First, add the following under the application node in Androidmanifest:
<activity
Android:name= ". Mainactivity "
Android:label= "@string/app_name" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Then, in the project, new one class, Musicserver:
Package Com.example.flybird_anla;
Import Android.app.Service;
Import android.content.Intent;
Import Android.media.MediaPlayer;
Import Android.os.IBinder;
public class Musicserver extends Service {
Private MediaPlayer MediaPlayer;
@Override
Public IBinder Onbind (Intent Intent) {
TODO auto-generated Method Stub
return null;
}
@Override
public void OnStart (Intent Intent, int startid) {
Super.onstart (Intent, Startid);
if (MediaPlayer = = null) {
R.RAW.MMP is a resource file in MP3 format.
MediaPlayer = Mediaplayer.create (this, r.raw.paopao);
Mediaplayer.setlooping (TRUE);
Mediaplayer.start ();
}
}
@Override
public void OnDestroy () {
TODO auto-generated Method Stub
Super.ondestroy ();
Mediaplayer.stop ();
}
}
Finally, in activity, write two functions:
protected void OnCreate (Bundle savedinstancestate) {
Intent Intent = new Intent (mainactivity.this,musicserver.class);
Super.oncreate (savedinstancestate);
StartService (Intent);
Manu ();
}
protected void OnStop () {
Intent Intent = new Intent (mainactivity.this,musicserver.class);
StopService (Intent);
Super.onstop ();
}
Add background music to your app