android的service学习案例------自己做的音乐播放器,让service后台播放mp3文件(是burning.mp3哦!)
2012-07-30 11:06
369 查看
当需要创建在后台运行的程序的时候,就要使用到Service。Service 可以分为有无限生命和有限生命两种。特别需要注意的是Service跟Activities是不同的(简单来说可以理解为后台与前台的区别),例如,如果
需要使用Service的话,需要调用startService(),从而利用startService()去调用Service中的 OnCreate()和onStart()方法来启动一个后台的Service。
启动一个Service的过程如
下:context.startService() ->onCreate()- >onStart()->Service running其中onCreate()可以进行一些服务的初始化工作,onStart()则启动服务。
停止一个Service的过程如下:context.stopService() | ->onDestroy() ->Service stop
接下来的实例是一个利用后台服务播放音乐的小例子,点击start运行服务,点击stop停止服务。
MainActivity的源代码:
package com.example.test1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button playButton = null;
private Button stopButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playButton = (Button)findViewById(R.id.startPlayButton);
stopButton = (Button)findViewById(R.id.stopPlayButton);
playButton.setOnClickListener(new ButtonListen());
stopButton.setOnClickListener(new ButtonListen());
}
class ButtonListen implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.startPlayButton:
startService(new Intent(MainActivity.this, MyService.class));
break;
case R.id.stopPlayButton:
stopService(new Intent(MainActivity.this, MyService.class));
break;
default:
break;
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
MyService的源代码:
package com.example.test1;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
MediaPlayer player = null;
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(this,
"MyService Createed!",
Toast.LENGTH_SHORT).show();
player = MediaPlayer.create(this, R.raw.burning);
player.setLooping(false);
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
Toast.makeText(this,
"MyService onStart!",
Toast.LENGTH_SHORT).show();
player.start();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
player.stop();
Toast.makeText(this,
"MyService onDestroy",
Toast.LENGTH_SHORT).show();
}
}
源码稍后提供下载:http://download.csdn.net/detail/yanzi1225627/4463948
需要使用Service的话,需要调用startService(),从而利用startService()去调用Service中的 OnCreate()和onStart()方法来启动一个后台的Service。
启动一个Service的过程如
下:context.startService() ->onCreate()- >onStart()->Service running其中onCreate()可以进行一些服务的初始化工作,onStart()则启动服务。
停止一个Service的过程如下:context.stopService() | ->onDestroy() ->Service stop
接下来的实例是一个利用后台服务播放音乐的小例子,点击start运行服务,点击stop停止服务。
MainActivity的源代码:
package com.example.test1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button playButton = null;
private Button stopButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playButton = (Button)findViewById(R.id.startPlayButton);
stopButton = (Button)findViewById(R.id.stopPlayButton);
playButton.setOnClickListener(new ButtonListen());
stopButton.setOnClickListener(new ButtonListen());
}
class ButtonListen implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.startPlayButton:
startService(new Intent(MainActivity.this, MyService.class));
break;
case R.id.stopPlayButton:
stopService(new Intent(MainActivity.this, MyService.class));
break;
default:
break;
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
MyService的源代码:
package com.example.test1;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
MediaPlayer player = null;
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(this,
"MyService Createed!",
Toast.LENGTH_SHORT).show();
player = MediaPlayer.create(this, R.raw.burning);
player.setLooping(false);
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
Toast.makeText(this,
"MyService onStart!",
Toast.LENGTH_SHORT).show();
player.start();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
player.stop();
Toast.makeText(this,
"MyService onDestroy",
Toast.LENGTH_SHORT).show();
}
}
源码稍后提供下载:http://download.csdn.net/detail/yanzi1225627/4463948
相关文章推荐
- Android-Service (基本知识,生命周期,实例-startService 启动的服务音乐播放器后台服务播放)
- Android service学习总结和利用service实现的后台音乐播放功能
- Android-Service (基本知识,生命周期,实例-startService 启动的服务音乐播放器后台服务播放)
- Android 音乐播放器,Service后台管理播放
- 【Android】简易音乐播放器(介绍使用Service和Broadcast播放音乐文件)
- 【Android】简易音乐播放器(介绍使用Service和Broadcast播放音乐文件)
- Android系列之音乐播放器的实现一(播放手机内存音频文件)
- 深度分析:Android4.3下MMS发送到附件为音频文件(音频为系统内置音频)的彩信给自己,添加音频-发送彩信-接收彩信-下载音频附件-预览-播放(三,接收彩信<1,接收短信>)
- 深度分析:Android4.3下MMS发送到附件为音频文件(音频为系统内置音频)的彩信给自己,添加音频-发送彩信-接收彩信-下载音频附件-预览-播放(一,添加附件)
- android多线程:自己撰写Proxy/Stub播放MP3音乐
- 深度分析:Android4.3下MMS发送到附件为音频文件(音频为系统内置音频)的彩信给自己,添加音频-发送彩信-接收彩信-下载音频附件-预览-播放(二,发送彩信<2>)
- Android学习小记-----监听并保存传感器数据,让service后台运行(保持CPU运转
- Android 初步学习BroadCast与Service实现简单的音乐播放器
- Android多媒体学习六:利用Service实现背景音乐的播放
- 深度分析:Android4.3下MMS发送到附件为音频文件(音频为系统内置音频)的彩信给自己,添加音频-发送彩信-接收彩信-下载音频附件-预览-播放(一,添加附件)
- 初级音乐播放器——播放存储卡上的歌曲,能够播放和暂停(SD卡没有MP3文件会报错)
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
- Android实现后台播放音乐(Service方式)
- Android四大组件--Service,后台播放背景音乐
- Android MediaPlayer 简单综合应用------列出sdcard里所有.mp3文件,并且可以点击播放!