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

Android Service的使用方法 音乐播放器实例

2015-05-31 20:09 489 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: