移动应用服务器端开发(基于JSP技术)-2017 Service 实验报告:Service.docVIP

  • 21
  • 0
  • 约3.21千字
  • 约 5页
  • 2021-01-28 发布于北京
  • 举报

移动应用服务器端开发(基于JSP技术)-2017 Service 实验报告:Service.doc

服务(Service) PAGE 5 实训(实 验) 报 告 实训(实验)项目: 服务相关属性使用 实验目的: 学习服务相关属性 学习服务相关属性与使用 实训(实验)设计(器材、步骤、结论): 简介: 服务是Android中实现程序后台运行的解决方案,它非常适合去执行那些不需要和用户交互而且还要长期运行的任务,即使被切换到后台,或者用户打开了另一个应用程序,服务仍然能够保持正常运行。 实验目的 : 使用服务播放音乐 任务 :使用我们学习服务的知识点,在MainActivity中添加音乐播放功能。 步骤1.修改activity_main布局代码。 LinearLayout xmlns:android=/apk/res/android android:layout_width=match_parent android:layout_height=match_parent android:orientation=vertical Button android:id=@+id/start_music android:layout_width=match_parent android:layout_height=wrap_content android:onClick=onClick android:text=播放音乐/ Button android:id=@+id/stop_music android:layout_width=match_parent android:layout_height=wrap_content android:onClick=onClick android:text=停止播放/ /LinearLayout 步骤 2.创建MusicService类文件。 代码: public class MusicService extends Service { private MediaPlayer player; public MusicService() { } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException(Not yet implemented); } @Override public void onCreate() { super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(cat,onStartCommand); //这里可以理解为装载音乐文件 player = MediaPlayer.create(this, R.raw.music); //开始播放 player.start(); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { //停止播放 player.stop(); //释放资源 player.release(); Log.d(cat,onDestroy); super.onDestroy(); } } 代码解析:我们在onStartCommand中添加了我们的播放源,也就是这一行player = MediaPlayer.create(this, R.raw.music);,需要注意的是你要在res文件夹下创建raw文件夹将你要使用的mp3音频文件放入其中,这里R.raw.music,music是我重新命名的音乐文件名。 步骤3. 修改MainActivity代码。 代码: public class M

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档