原文地址:http://www.cnblogs.com/qq78292959/archive/2011/12/02/2272008.html

Service翻译成中文是服务,熟悉Windows 系统的同学一定知道很熟悉了。Android里的Service跟Windows里的Service功能差不多,就是一个不可见的进程在后台执行,避免被用户误关闭。因为Android在某些情况下会自动关闭非前台显示的Activity,所以如果要让一个功能在后台一直执行,不被Android系统关闭,比如说闹钟、后台播放音乐,就必须使用Service.
之前开发音乐播放器的时候也没用Service,但是却可以后台播放,以为Service没什么用,但是经过一段时间后发现,没用Service的播放器在播放一段时间后会被系统自动关闭,而且就算还在后台播放,过一段时间后打开播放器,再点播放按钮,会出现两种声音,今天用Service写了个Demo,完美解决以上问题。// 饭店123按:我的播放器也是不用Service,也能退出后播放,再打开播放,确实会出现两个声音因为有两个MediaPlayer
布局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>
复制代码

© 2010, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记

更多相关文章

  1. Capacitor实现WebView中访问的自定义Android代码
  2. 【Android】系统构架
  3. linux系统和android系统的区别
  4. 分别在Linux和Android中用C语言写系统日志
  5. Android系统中UID
  6. android之使用signapk打包成系统应用,获取系统权限

随机推荐

  1. Android 文件存储的简单实现
  2. Android Password Manager
  3. Android之SurfaceView实现视频播放
  4. android 保持屏幕唤醒状态
  5. 「React Native」Android返回键监听
  6. android activity FLAG_ACTIVITY_CLEAR_T
  7. android 4.0 兼容性问题 java.lang.NoSuc
  8. Android多媒体开发 Pro Android(安卓)Med
  9. Android中的图片处理——色彩、形状拉伸
  10. 自定义RatingBar