Android Service的使用方法 音乐播放器实例
来源:互联网 发布:打印机的端口怎么看 编辑:程序博客网 时间:2024/05/22 09:54
Service翻译成中文是服务,熟悉Windows 系统的同学一定知道很熟悉了。Android里的Service跟Windows里的Service功能差不多,就是一个不可见的进程在后台执行,避免被用户误关闭。因为Android在某些情况下会自动关闭非前台显示的Activity,所以如果要让一个功能在后台一直执行,不被Android系统关闭,比如说闹钟、后台播放音乐,就必须使用Service.
之前开发音乐播放器的时候也没用Service,但是却可以后台播放,以为Service没什么用,但是经过一段时间后发现,没用Service的播放器在播放一段时间后会被系统自动关闭,而且就算还在后台播放,过一段时间后打开播放器,再点播放按钮,会出现两种声音,今天用Service写了个Demo,完美解决以上问题。
布局XML,就两个按钮:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><Button android:id="@+id/start" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="开始" /> <Button android:id="@+id/stop" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="停止" /></LinearLayout>
主程序main.java:
package com.pocketdigi.service;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class main extends Activity { /** Called when the activity is first created. */ Button start,stop; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findView(); start.setOnClickListener(startlis); stop.setOnClickListener(stoplis); } private OnClickListener startlis=new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub startService(new Intent(main.this, Music.class)); //启动服务 } }; private OnClickListener stoplis=new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub stopService(new Intent(main.this,Music.class)); //停止服务 } }; public void findView(){ start=(Button)findViewById(R.id.start); stop=(Button)findViewById(R.id.stop); }}
服务 Music.java:
package com.pocketdigi.service;import android.app.Service;import android.content.Intent;import android.media.MediaPlayer;import android.os.IBinder;public class Music extends Service { private MediaPlayer mp; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { super.onCreate(); mp=MediaPlayer.create(this,R.raw.xrx); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); mp.start(); } @Override public void onDestroy() { super.onDestroy(); mp.stop(); }}
服务还需要在AndroidManifest.xml注册后才能使用:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.pocketdigi.service" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".main" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:enabled="true" android:name=".Music" /> </application></manifest>
0 0
- Android Service的使用方法 音乐播放器实例
- Android Service的使用方法 音乐播放器实例
- Android Service的使用方法 音乐播放器实例
- Android Service的使用方法 音乐播放器实例
- Android Service使用方法--简单音乐播放实例
- Android Service使用方法--简单音乐播放实例
- Android Service使用方法--简单音乐播放实例
- Android Service使用方法--简单音乐播放实例
- Android Service使用方法--简单音乐播放实例
- Android Service使用方法--简单音乐播放实例
- Android Service使用方法--简单音乐播放实例
- android,service实例,播放音乐
- Android音乐播放器的播放实例
- Android-Service (基本知识,生命周期,实例-startService 启动的服务音乐播放器后台服务播放)
- Android-Service (基本知识,生命周期,实例-startService 启动的服务音乐播放器后台服务播放)
- Android的音乐播放器使用service
- Service播放音乐实例
- android-service音乐播放器
- Codeforces Round #306 (Div. 2)D
- Lua与.net的CLR相互调用
- leecode 解题总结:268. Missing Number
- pat 1007. Maximum Subsequence Sum (25)
- libsvm-3.22与MATLAB2014a中svmtrian()函数冲突问题的解决
- Android Service的使用方法 音乐播放器实例
- 常见的图片上的描述
- 并查集--九度1012 & 1109 [路径压缩]
- Archlinux作为Openstreetmap服务器滚动更新
- php正则语法--分隔符
- 随手记,一些bug的调试
- 冒泡排序
- 零基础写Java爬虫
- centos7下安装jdk(rpm)