您的位置:首页 > 移动开发 > Android开发

Android四大组件之------Service(本地服务小案例)

2016-10-27 23:24 453 查看
很多童鞋被Service的本地服务与远程服务所困扰,下面就写一个小Demo来展示本地服务跟远程服务的区别:

一、概念:本地服务是Service对象与Service的启动者在同个进程中运行, 两者的通信是进程内通信。

二、这里做的本地服务呢是在本地服务中实现一个音乐的播放,废话不多说,直接上代码:

activity_main:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
android:id="@+id/btn_main_play"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="play"
android:layout_marginTop="20dp"/>

<Button
android:id="@+id/btn_main_stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="stop"
android:layout_marginTop="20dp"/>

<Button
android:id="@+id/btn_main_pause"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="pause"
android:layout_marginTop="20dp"/>

<Button
android:id="@+id/btn_main_exit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="exit"
android:layout_marginTop="20dp"/>
</LinearLayout>


MainActivity:

public class MainActivity extends Activity implements View.OnClickListener {

private Button btn_main_play;
private Button btn_main_stop;
private Button btn_main_pause;
private Button btn_main_exit;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btn_main_play = (Button)findViewById(R.id.btn_main_play);
btn_main_stop = (Button)findViewById(R.id.btn_main_stop);
btn_main_pause = (Button)findViewById(R.id.btn_main_pause);
btn_main_exit = (Button)findViewById(R.id.btn_main_exit);

btn_main_play.setOnClickListener(this);
btn_main_stop.setOnClickListener(this);
btn_main_pause.setOnClickListener(this);
btn_main_exit.setOnClickListener(this);
}

@Override
public void onClick(View v) {
if(v == btn_main_play) {
Intent intent = new Intent(this,LocalService.class);
intent.putExtra("action","play");
startService(intent);
}else if(v == btn_main_pause) {

Intent intent = new Intent(this,LocalService.class);
intent.putExtra("action","pause");
startService(intent);

}else if(v == btn_main_stop) {

Intent intent = new Intent(this,LocalService.class);
intent.putExtra("action","stop");
startService(intent);

}else if(v == btn_main_exit) {

Intent intent = new Intent(this,LocalService.class);
stopService(intent);

finish();

}
}
}


LocalService:

public class LocalService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

String action = intent.getStringExtra("action");
if("play".equals(action)) {
play();
}else if("pause".equals(action)) {
pause();
}else if("stop".equals(action)) {
stop();
}

return super.onStartCommand(intent, flags, startId);
}

private MediaPlayer player;
//停止
private void stop() {
Log.e("TAG", "停止播放");
if(player != null) {
player.stop();
player.reset();
player.release();
player = null;
}

}

//暂停
private void pause() {
if(player!=null && player.isPlaying()) {
player.pause();
}
}

//播放
private void play() {

if(player == null) {
player = MediaPlayer.create(this,R.raw.water_hander);
}
Log.e("TAG", "播放");
if(!player.isPlaying()) {
player.start();
}
}

@Override
public void onDestroy() {
stop();
super.onDestroy();
}
}


最后不要忘记在AndroidManifest中注册Service并且要在res目录下添加raw资源文件夹,放入要播放的音乐。

这些代码,就可以轻松实现音乐的播放梦想了!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Service android 音乐