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>


© 2010, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接![url] http://www.pocketdigi.com/ 应用开发笔记[/url]

更多相关文章

  1. 浅析android通过jni控制service服务程序的简易流程
  2. Android(安卓)Service AIDL
  3. Android(安卓)Audio Focus的应用(requestAudioFocus)
  4. Android架构分析之使用自定义硬件抽象层(HAL)模块
  5. android 定位服务
  6. 【安卓笔记】android客户端与服务端交互的三种方式
  7. android手机客户端上传文件,java servlet服务器端接收并保存到服
  8. Android(安卓)后台任务(五)Service
  9. android 定位服务

随机推荐

  1. 【Android】Broadcast控制音乐暂停继续等
  2. Android(安卓)UI 用户界面开发基本概念概
  3. Android实现无标题栏全屏的方法
  4. android gif动画
  5. android的listView组件
  6. Android扫描条形码实现
  7. Android开机LOG
  8. Seekbar thumb滑动时上下显示不全,或者左
  9. Android(安卓)实现TextView中文字链接的
  10. Android(安卓)之 SystemService