Android Learning Organization _ Service simple application. Music player
1. Because we need to use Service, we need to define it in the AndroidManifest. xml file.
<Service android: name = ". Music"> <! -- We will set a music. java file to implement -->
<Intent-filter>
<Action android: name = "com. edgar. PlayService. START_AUDIO_SERVICE"/> <! -- We will specify the name of the action that calls music. java.
<Category android: name = "Android. intent. category. default"/>
</Intent-filter>
</Service> remember to comment the code in the <application> label. Do not leave it outside. An error will be reported outside.
2. Then, put the music to be played in the res/raw folder.
3.
// Music. java File
Package com. edgar. playservice;
Import com. edgar. playservice. R;
Import com. edgar. playservice. R. raw;
Import android. app. Service;
Import android. content. Intent;
Import android. media. MediaPlayer;
Import android. OS. IBinder;
Import android. text. style. SuperscriptSpan;
Public class Music extends Service {
Private MediaPlayer player;
@ Override
Public IBinder onBind (Intent intent ){
// The method stub automatically generated by TODO
Return null;
}
Public void onStart (Intent intent, int startId ){
Super. onStart (intent, startId );
Player = MediaPlayer. create (this, R. raw. gequ); // gequ is the song name.
Player. start ();
}
Public void onDestroy (){
Super. onDestroy ();
Player. stop ();
}
}
// MainActivity. java File
Package com. edgar. playservice;
Import Android. EdgarModel. Msg;
Import android. OS. Bundle;
Import android. app. Activity;
Import android. content. Intent;
Import android. view. Menu;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. Toast;
Public class MainActivity extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Button btnStart = (Button) findViewById (R. id. BtnStart );
Button btnStop = (Button) findViewById (R. id. BtnStop );
// Button btnPause = (Button) findViewById (R. id. BtnPause );
BtnStart. setOnClickListener (startListener );
BtnStop. setOnClickListener (stopListener );
// BtnPause. setOnClickListener (pauseListener );
}
// Private OnClickListener pauseListener = new OnClickListener (){
//
// @ Override
// Public void onClick (View v ){
//// Method stub automatically generated by TODO
// Msg. show (MainActivity. this, "pause ");
//}
//};
Private OnClickListener stopListener = new OnClickListener (){
@ Override
Public void onClick (View v ){
// The method stub automatically generated by TODO
Toast. makeText (MainActivity. this, "stop", Toast. LENGTH_SHORT)
. Show ();
StopService (new Intent ("com. edgar. PlayService. START_AUDIO_SERVICE "));
Finish ();
}
};
Private OnClickListener startListener = new OnClickListener (){
@ Override
Public void onClick (View v ){
// The method stub automatically generated by TODO
Toast. makeText (MainActivity. this, "start", Toast. LENGTH_SHORT)
. Show ();
StartService (new Intent ("com. edgar. PlayService. START_AUDIO_SERVICE "));
}
};
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// Inflate the menu; this adds items to the action bar if it is present.
GetMenuInflater (). inflate (R. menu. main, menu );
Return true;
}
}