首頁(yè) 收藏 QQ群
 網(wǎng)站導(dǎo)航

ZNDS智能電視網(wǎng) 推薦當(dāng)貝市場(chǎng)

TV應(yīng)用下載 / 資源分享區(qū)

軟件下載 | 游戲 | 討論 | 電視計(jì)算器

綜合交流 / 評(píng)測(cè) / 活動(dòng)區(qū)

交流區(qū) | 測(cè)硬件 | 網(wǎng)站活動(dòng) | Z幣中心

新手入門(mén) / 進(jìn)階 / 社區(qū)互助

新手 | 你問(wèn)我答 | 免費(fèi)刷機(jī)救磚 | ROM固件

查看: 11268|回復(fù): 0
上一主題 下一主題
[教程]

第十四講:Service入門(mén)指南

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
發(fā)表于 2013-8-28 16:19 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
680本講內(nèi)容:Service   
1、Service的概念   
2、Service的生命周期   
3、實(shí)例:控制音樂(lè)播放的Service
本講源代碼:
  Service對(duì)象不能自己?jiǎn)?dòng),需要通過(guò)某個(gè)Activity、Service或者其他Context對(duì)象來(lái)啟動(dòng)。啟動(dòng)的方法有兩種,Context.startService和Context.bindService()。兩種方式的生命周期是不同的,具體如下所示。Context.startService方式的生命周期:   
啟動(dòng)時(shí),startService –> onCreate() –> onStart()   
停止時(shí),stopService –> onDestroy()Context.bindService方式的生命周期:   
綁定時(shí),bindService  -> onCreate() –> onBind()   
解綁定時(shí),unbindService –>onUnbind() –> onDestory()下面我們用一個(gè)可以控制在后臺(tái)播放音樂(lè)的例子來(lái)演示剛才所學(xué)知識(shí),同學(xué)們可以通過(guò)該例子可以明顯看到通過(guò)綁定方式運(yùn)行的Service在綁定對(duì)象被銷毀后也被銷毀了。
  下面把代碼分享如下:1、建立一個(gè)新項(xiàng)目名字叫 Lesson14_HelloService,Activity起名叫MainHelloService.java2、res/layout/main.xml中代碼寫(xiě)成   
  1. < ?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">   
    <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="音樂(lè)播放服務(wù)" android:textsize="25sp" android:layout_margintop="10dp">   
    <button android:text="開(kāi)啟音樂(lè)播放服務(wù)" android:textsize="20sp" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="10dp">   
    </button>   
    <button android:text="停止音樂(lè)播放服務(wù)" android:textsize="20sp" android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="10dp">   
    </button>   
       
    <button android:text="綁定音樂(lè)播放服務(wù)" android:textsize="20sp" android:id="@+id/Button03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="10dp">   
    </button>   
    <button android:text="解綁定音樂(lè)播放服務(wù)" android:textsize="20sp" android:id="@+id/Button04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="10dp">   
    </button>   
    </textview></linearlayout>   
復(fù)制代碼
2、在res目錄中建立一個(gè)raw目錄,并把一個(gè)音樂(lè)文件babayetu.mp3拷貝進(jìn)來(lái)3、在Activity的同目錄新建一個(gè)service文件MusicService.java   
  1. package android.basic.lesson14;   
       
    import android.app.Service;   
    import android.content.Intent;   
    import android.media.MediaPlayer;   
    import android.os.IBinder;   
    import android.util.Log;   
    import android.widget.Toast;   
       
    public class MusicService extends Service {   
       
            //為日志工具設(shè)置標(biāo)簽   
            String tag ="MusicService";           
       
            //定義音樂(lè)播放器變量   
            MediaPlayer mPlayer;   
       
            //其他對(duì)象通過(guò)bindService方法通知該Service時(shí)該方法會(huì)被調(diào)用   
            @Override   
            public IBinder onBind(Intent intent) {   
                    Toast.makeText(this,"MusicService onBind()",Toast.LENGTH_SHORT).show();   
                    Log.i(tag, "MusicService onBind()");   
                    mPlayer.start();   
                    return null;   
            }   
       
            //其他對(duì)象通過(guò)unbindService方法通知該Service時(shí)該方法會(huì)被調(diào)用   
            @Override   
            public boolean onUnbind(Intent intent){   
                    Toast.makeText(this, "MusicService onUnbind()", Toast.LENGTH_SHORT).show();   
                    Log.i(tag, "MusicService onUnbind()");   
                    mPlayer.stop();   
                    return false;   
            }   
       
            //該服務(wù)不存在需要被創(chuàng)建時(shí)被調(diào)用,不管startService()還是bindService()都會(huì)在啟動(dòng)時(shí)調(diào)用該方法   
            @Override   
            public void onCreate(){   
                    Toast.makeText(this, "MusicService onCreate()", Toast.LENGTH_SHORT).show();   
                    //創(chuàng)建一個(gè)音樂(lè)播放器對(duì)象   
                    mPlayer=MediaPlayer.create(getApplicationContext(), R.raw.babayetu);   
                    //設(shè)置可以重復(fù)播放   
                    mPlayer.setLooping(true);   
                    Log.i(tag, "MusicService onCreate()");   
            }   
       
            //用startService方法調(diào)用該服務(wù)時(shí),在onCreate()方法調(diào)用之后,會(huì)調(diào)用改方法   
            @Override   
            public void onStart(Intent intent,int startid){   
                    Toast.makeText(this,"MusicService onStart",Toast.LENGTH_SHORT).show();   
                    Log.i(tag, "MusicService onStart()");   
                    mPlayer.start();   
            }   
       
            //該服務(wù)被銷毀時(shí)調(diào)用該方法   
            @Override   
            public void onDestroy(){   
                    Toast.makeText(this, "MusicService onDestroy()", Toast.LENGTH_SHORT).show();   
                    mPlayer.stop();   
                    Log.i(tag, "MusicService onDestroy()");   
            }   
    }
復(fù)制代碼
4、MainHelloService.java中的代碼:   
  1. package android.basic.lesson14;   
       
    import android.app.Activity;   
    import android.content.ComponentName;   
    import android.content.Context;   
    import android.content.Intent;   
    import android.content.ServiceConnection;   
    import android.os.Bundle;   
    import android.os.IBinder;   
    import android.util.Log;   
    import android.view.View;   
    import android.view.View.OnClickListener;   
    import android.widget.Button;   
    import android.widget.Toast;   
       
    public class MainHelloService extends Activity {   
       
            //為日志工具設(shè)置標(biāo)簽   
            String tag = "MusicService";   
       
        /** Called when the activity is first created. */   
        @Override   
        public void onCreate(Bundle savedInstanceState) {   
            super.onCreate(savedInstanceState);   
            setContentView(R.layout.main);   
       
            //輸出Toast消息和日志記錄   
                    Toast.makeText(MainHelloService.this, "MainHelloService onCreate", Toast.LENGTH_SHORT).show();   
                    Log.i(tag, "MainHelloService onCreate");   
       
            //定義組件對(duì)象   
            Button b1= (Button)findViewById(R.id.Button01);   
            Button b2= (Button)findViewById(R.id.Button02);   
            Button b3= (Button)findViewById(R.id.Button03);   
            Button b4= (Button)findViewById(R.id.Button04);   
       
             //定義服務(wù)鏈接對(duì)象   
             final ServiceConnection conn = new ServiceConnection(){   
       
                            @Override   
                            public void onServiceConnected(ComponentName name, IBinder service) {   
                                    Toast.makeText(MainHelloService.this, "ServiceConnection onServiceConnected", Toast.LENGTH_SHORT).show();   
                                    Log.i(tag, "ServiceConnection onServiceConnected");   
       
                            }   
       
                            @Override   
                            public void onServiceDisconnected(ComponentName name) {   
                                    Toast.makeText(MainHelloService.this, "ServiceConnection onServiceDisconnected", Toast.LENGTH_SHORT).show();   
                                    Log.i(tag, "ServiceConnection onServiceDisconnected");   
       
                            }};   
       
                    //定義點(diǎn)擊監(jiān)聽(tīng)器   
            OnClickListener ocl= new OnClickListener(){   
       
                            @Override   
                            public void onClick(View v) {   
                                    //顯示指定intent所指的對(duì)象是個(gè)Service   
                                    Intent intent = new Intent(MainHelloService.this,android.basic.lesson14.MusicService.class);   
                                    switch(v.getId()){   
                                    case R.id.Button01:   
                                            //開(kāi)始服務(wù)   
                                            startService(intent);   
                                            break;   
                                    case R.id.Button02:   
                                            //停止服務(wù)   
                                            stopService(intent);   
                                            break;   
                                    case R.id.Button03:   
                                            //綁定服務(wù)   
                                            bindService(intent,conn,Context.BIND_AUTO_CREATE);   
                                            break;   
                                    case R.id.Button04:   
                                            //解除綁定   
                                            unbindService(conn);   
                                            break;   
                                    }   
                            }   
            };   
       
            //綁定點(diǎn)擊監(jiān)聽(tīng)器   
            b1.setOnClickListener(ocl);   
            b2.setOnClickListener(ocl);   
            b3.setOnClickListener(ocl);   
            b4.setOnClickListener(ocl);      
       
        }   
       
        @Override   
        public void onDestroy(){   
                super.onDestroy();   
                    Toast.makeText(MainHelloService.this, "MainHelloService onDestroy", Toast.LENGTH_SHORT).show();   
                    Log.i(tag, "MainHelloService onDestroy");   
        }   
    }
復(fù)制代碼
  
   好了,本講就到這里。   

上一篇:Android游戲開(kāi)發(fā)之小球重力感應(yīng)實(shí)現(xiàn)(二十五)
下一篇:第十三講:用戶界面 View(八)

本版積分規(guī)則

Archiver|新帖|標(biāo)簽|軟件|Sitemap|ZNDS智能電視網(wǎng) ( 蘇ICP備2023012627號(hào) )

網(wǎng)絡(luò)信息服務(wù)信用承諾書(shū) | 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證:蘇B2-20221768 丨 蘇公網(wǎng)安備 32011402011373號(hào)

GMT+8, 2024-10-21 04:00 , Processed in 0.065154 second(s), 15 queries , Redis On.

Powered by Discuz!

監(jiān)督舉報(bào):report#znds.com (請(qǐng)將#替換為@)

© 2007-2024 ZNDS.Com

快速回復(fù) 返回頂部 返回列表