欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android Service詳解及示例代碼

 更新時間:2016年08月10日 09:20:52   作者:chino  
本文主要介紹Android Service,在Android應用開發(fā)過程中,Service 會經常用到,這里對Service 的概念,生命周期等做了詳細介紹,并附示例代碼,有需要的朋友可以參考下

Android Service 詳細介紹:

1、Service的概念

2、Service的生命周期

3、實例:控制音樂播放的Service

一、Service的概念

Service是Android程序中四大基礎組件之一,它和Activity一樣都是Context的子類,只不過它沒有UI界面,是在后臺運行的組件。

二、Service的生命周期

Service對象不能自己啟動,需要通過某個Activity、Service或者其他Context對象來啟動。啟動的方法有兩種,Context.startService和Context.bindService()。兩種方式的生命周期是不同的,具體如下所示。

Context.startService方式的生命周期:

啟動時,startService –> onCreate() –> onStart()
停止時,stopService –> onDestroy()

Context.bindService方式的生命周期:

綁定時,bindService  -> onCreate() –> onBind()
解綁定時,unbindService –>onUnbind() –> onDestory()

三、實例:控制音樂播放的Service

下面我們用一個可以控制在后臺播放音樂的例子來演示剛才所學知識,同學們可以通過該例子可以明顯看到通過綁定方式運行的Service在綁定對象被銷毀后也被銷毀了。

 下面把代碼分享如下:

1、建立一個新項目名字叫 Lesson14_HelloService,Activity起名叫MainHelloService.java

2、res/layout/main.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">
<textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="音樂播放服務" android:textsize="25sp" android:layout_margintop="10dp">
<button android:text="開啟音樂播放服務" 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="停止音樂播放服務" 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="綁定音樂播放服務" 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="解綁定音樂播放服務" android:textsize="20sp" android:id="@+id/Button04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="10dp">
</button>
</textview></linearlayout>

3、在res目錄中建立一個raw目錄,并把一個音樂文件babayetu.mp3拷貝進來3、在Activity的同目錄新建一個service文件

MusicService.java

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 {

    //為日志工具設置標簽
    String tag ="MusicService";    

    //定義音樂播放器變量
    MediaPlayer mPlayer;

    //其他對象通過bindService方法通知該Service時該方法會被調用
    @Override
    public IBinder onBind(Intent intent) {
        Toast.makeText(this,"MusicService onBind()",Toast.LENGTH_SHORT).show();
        Log.i(tag, "MusicService onBind()");
        mPlayer.start();
        return null;
    }

    //其他對象通過unbindService方法通知該Service時該方法會被調用
    @Override
    public boolean onUnbind(Intent intent){
        Toast.makeText(this, "MusicService onUnbind()", Toast.LENGTH_SHORT).show();
        Log.i(tag, "MusicService onUnbind()");
        mPlayer.stop();
        return false;
    }

    //該服務不存在需要被創(chuàng)建時被調用,不管startService()還是bindService()都會在啟動時調用該方法
    @Override
    public void onCreate(){
        Toast.makeText(this, "MusicService onCreate()", Toast.LENGTH_SHORT).show();
        //創(chuàng)建一個音樂播放器對象
        mPlayer=MediaPlayer.create(getApplicationContext(), R.raw.babayetu);
        //設置可以重復播放
        mPlayer.setLooping(true);
        Log.i(tag, "MusicService onCreate()");
    }

    //用startService方法調用該服務時,在onCreate()方法調用之后,會調用改方法
    @Override
    public void onStart(Intent intent,int startid){
        Toast.makeText(this,"MusicService onStart",Toast.LENGTH_SHORT).show();
        Log.i(tag, "MusicService onStart()");
        mPlayer.start();
    }

    //該服務被銷毀時調用該方法
    @Override
    public void onDestroy(){
        Toast.makeText(this, "MusicService onDestroy()", Toast.LENGTH_SHORT).show();
        mPlayer.stop();
        Log.i(tag, "MusicService onDestroy()");
    }
}

4、MainHelloService.java中的代碼:

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 {

    //為日志工具設置標簽
    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");

    //定義組件對象
    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);

     //定義服務鏈接對象
     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");

            }};

        //定義點擊監(jiān)聽器
    OnClickListener ocl= new OnClickListener(){

            @Override
            public void onClick(View v) {
                //顯示指定intent所指的對象是個Service
                Intent intent = new Intent(MainHelloService.this,android.basic.lesson14.MusicService.class);
                switch(v.getId()){
                case R.id.Button01:
                    //開始服務
                    startService(intent);
                    break;
                case R.id.Button02:
                    //停止服務
                    stopService(intent);
                    break;
                case R.id.Button03:
                    //綁定服務
                    bindService(intent,conn,Context.BIND_AUTO_CREATE);
                    break;
                case R.id.Button04:
                    //解除綁定
                    unbindService(conn);
                    break;
                }
            }
    };

    //綁定點擊監(jiān)聽器
    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");
  }
}

以上就是對Android Service的介紹,后續(xù)繼續(xù)補充相關資料,謝謝大家對本站的支持!

相關文章

  • RxJava2配置及使用詳解

    RxJava2配置及使用詳解

    這篇文章主要介紹了RxJava2配置及使用詳解,RxJava2.0是一個非常棒的流式編程,有興趣的可以了解一下
    2017-06-06
  • 使用adb?or?fastboot命令進入高通的9008(edl)模式的兩種方法

    使用adb?or?fastboot命令進入高通的9008(edl)模式的兩種方法

    這篇文章主要介紹了使用adb?or?fastboot命令進入高通的9008(edl)模式,兩種方式通過命令給大家寫的非常詳細,文中又給大家補充介紹了高通手機?進入?高通9008模式的兩種方法,需要的朋友可以參考下
    2023-01-01
  • Android開發(fā)之獲取SD卡及手機ROM容量的方法

    Android開發(fā)之獲取SD卡及手機ROM容量的方法

    這篇文章主要介紹了Android開發(fā)之獲取SD卡及手機ROM容量的方法,結合實例形式分析了Android針對SD卡的讀取及屬性操作相關技巧,需要的朋友可以參考下
    2016-04-04
  • Android TextView和ImageView簡單說明

    Android TextView和ImageView簡單說明

    Android TextView和ImageView簡單說明,需要的朋友可以參考一下
    2013-03-03
  • Android Studio打包APK文件具體實現步驟解析

    Android Studio打包APK文件具體實現步驟解析

    這篇文章主要介紹了Android Studio打包APK文件具體實現步驟解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-11-11
  • 基于Android的服務器端程序實例

    基于Android的服務器端程序實例

    這篇文章主要介紹了基于Android的服務器端程序實例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • flutter日期選擇器 flutter時間選擇器

    flutter日期選擇器 flutter時間選擇器

    這篇文章主要為大家詳細介紹了flutter日期選擇器,flutter時間選擇器,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Android中EditText屏蔽第三方輸入法表情的方法示例

    Android中EditText屏蔽第三方輸入法表情的方法示例

    最近在工作終于遇到一個問題,因為第三方輸入法表情的問題導致Android中TextView的內容顯示異常,只能想辦法解決了,下面這篇文章主要記錄了在處理Android中EditText屏蔽第三方輸入法表情的方法,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-01-01
  • 最簡單的SD卡文件遍歷Android程序

    最簡單的SD卡文件遍歷Android程序

    這篇文章主要為大家詳細介紹了最簡單的SD卡文件遍歷Android程序,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Android編程之內存溢出解決方案(OOM)實例總結

    Android編程之內存溢出解決方案(OOM)實例總結

    這篇文章主要介紹了Android編程之內存溢出解決方案(OOM),結合實例實例總結分析了Android編程過程中常見的內存溢出情況與對應的解決方法,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11

最新評論