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

Android中Service服務(wù)詳解(一)

 更新時間:2016年01月15日 11:38:23   作者:殘缺的孤獨  
這篇文章主要介紹了Android中Service服務(wù),詳細(xì)介紹了Service服務(wù)的概念、功能及簡單使用方法,需要的朋友可以參考下

本文詳細(xì)分析了Android中Service服務(wù)。分享給大家供大家參考,具體如下:

一、Service簡介

Service是Android中實現(xiàn)程序后臺運行的解決方案,適用于去執(zhí)行那些不需要和用戶交互而且還要求長期運行的任務(wù)。Service是android 系統(tǒng)中的四大組件之一(Activity、Service、BroadcastReceiver、ContentProvider),它跟Activity的級別差不多,但不能自己運行只能后臺運行,并且可以和其他組件進(jìn)行交互。

Service并不是運行在一個獨立的進(jìn)程當(dāng)中的,而是依賴于創(chuàng)建服務(wù)時所在的應(yīng)用程序進(jìn)程。當(dāng)某個應(yīng)用程序進(jìn)程被殺掉時,所有依賴于該進(jìn)程的服務(wù)也會停止運行。

二、Service初實踐

創(chuàng)建一個Android項目TestService。

1、新建一個服務(wù)

package com.example.testservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
  @Override
  public IBinder onBind(Intent arg0) {
    return null;
  }
  /**
   * 服務(wù)第一次創(chuàng)建的時候調(diào)用
   */
  @Override
  public void onCreate() {
    super.onCreate();
    Toast.makeText(this, "服務(wù)的onCreate方法被調(diào)用", Toast.LENGTH_SHORT).show();
  }
  /**
   * 服務(wù)每一次啟動的時候調(diào)用
   */
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, "服務(wù)的onStartCommand方法被調(diào)用", Toast.LENGTH_SHORT).show();
    return super.onStartCommand(intent, flags, startId);
  }
  @Override
  public void onDestroy() {
    Toast.makeText(this, "服務(wù)的onDestroy方法被調(diào)用", Toast.LENGTH_SHORT).show();
    super.onDestroy();
  }
}

在創(chuàng)建一個服務(wù)時,繼承Service類,重寫了onCreate方法,onStartCommand方法以及onDestroy方法。

2、修改AndroidManifest.xml

當(dāng)新建完一個服務(wù)后,需要在AndroidManifest.xml中進(jìn)行注冊,如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.testservice"
  android:versionCode="1"
  android:versionName="1.0" >
  <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
      android:name="com.example.testservice.MainActivity"
      android:label="@string/app_name" >
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <!-- 注冊服務(wù) -->
    <service android:name=".MyService"></service>
  </application>
</manifest>

3、布局文件activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >
  <Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="啟動服務(wù)" />
  <Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="停止服務(wù)" />
</LinearLayout>

4、MainActivity.java文件

package com.example.testservice;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
  private Button startService_Button;
  private Button stopService_Button;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //獲取開啟服務(wù)按鈕
    startService_Button = (Button) findViewById(R.id.button1);
    //獲取停止服務(wù)按鈕
    stopService_Button = (Button) findViewById(R.id.button2);
    //調(diào)用點擊事件
    startService_Button.setOnClickListener(this);
    stopService_Button.setOnClickListener(this);
  }
  /**
   * 點擊事件
   */
  @Override
  public void onClick(View view) {
    switch(view.getId()){
    case R.id.button1:
      //"開啟服務(wù)"按鈕
      Intent startIntent = new Intent(this,MyService.class);
      //開啟服務(wù)
      startService(startIntent);
      break;
    case R.id.button2:
      //"停止服務(wù)"按鈕
      Intent stopIntent = new Intent(this,MyService.class);
      //停止服務(wù)
      stopService(stopIntent);
      break;
    default:
      break;
    }
  }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }
}

三、測試結(jié)果

發(fā)布項目后,如下所示:

當(dāng)點擊"啟動服務(wù)"按鈕后,會依次彈出如下:

 

并且,此時你多次點擊"啟動服務(wù)"按鈕,只會彈出上方右圖,而不再彈出上方左圖。因為僅僅在服務(wù)創(chuàng)建的時候會調(diào)用onCreate方法,但當(dāng)服務(wù)啟動的時候每次都會調(diào)用onStartCommand方法。

當(dāng)點擊"停止服務(wù)"后,如下:

總結(jié):Android Service服務(wù)的啟動流程如下:

調(diào)用Context的startService方法---》onCreate方法---》onStartCommand方法---》服務(wù)運行。

Android服務(wù)的停止流程如下:

服務(wù)運行---》調(diào)用Context的stopService方法--》onDestroy方法---》服務(wù)停止。

更多關(guān)于Android組件相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android基本組件用法總結(jié)

希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

最新評論