国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android中Service服務詳解(一)

2020-04-11 10:58:42
字體:
來源:轉載
供稿:網友

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

一、Service簡介

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

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

二、Service初實踐

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

1、新建一個服務

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;  }  /**   * 服務第一次創(chuàng)建的時候調用   */  @Override  public void onCreate() {    super.onCreate();    Toast.makeText(this, "服務的onCreate方法被調用", Toast.LENGTH_SHORT).show();  }  /**   * 服務每一次啟動的時候調用   */  @Override  public int onStartCommand(Intent intent, int flags, int startId) {    Toast.makeText(this, "服務的onStartCommand方法被調用", Toast.LENGTH_SHORT).show();    return super.onStartCommand(intent, flags, startId);  }  @Override  public void onDestroy() {    Toast.makeText(this, "服務的onDestroy方法被調用", Toast.LENGTH_SHORT).show();    super.onDestroy();  }}

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

2、修改AndroidManifest.xml

當新建完一個服務后,需要在AndroidManifest.xml中進行注冊,如下:

<?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>    <!-- 注冊服務 -->    <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="啟動服務" />  <Button    android:id="@+id/button2"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="停止服務" /></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);    //獲取開啟服務按鈕    startService_Button = (Button) findViewById(R.id.button1);    //獲取停止服務按鈕    stopService_Button = (Button) findViewById(R.id.button2);    //調用點擊事件    startService_Button.setOnClickListener(this);    stopService_Button.setOnClickListener(this);  }  /**   * 點擊事件   */  @Override  public void onClick(View view) {    switch(view.getId()){    case R.id.button1:      //"開啟服務"按鈕      Intent startIntent = new Intent(this,MyService.class);      //開啟服務      startService(startIntent);      break;    case R.id.button2:      //"停止服務"按鈕      Intent stopIntent = new Intent(this,MyService.class);      //停止服務      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;  }}

三、測試結果

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

當點擊"啟動服務"按鈕后,會依次彈出如下:

 

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

當點擊"停止服務"后,如下:

總結:Android Service服務的啟動流程如下:

調用Context的startService方法---》onCreate方法---》onStartCommand方法---》服務運行。

Android服務的停止流程如下:

服務運行---》調用Context的stopService方法--》onDestroy方法---》服務停止。

更多關于Android組件相關內容感興趣的讀者可查看本站專題:《Android基本組件用法總結

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 酒泉市| 沧州市| 剑阁县| 平江县| 遂平县| 巧家县| 藁城市| 青田县| 富锦市| 英超| 紫云| 开江县| 平阴县| 五大连池市| 平邑县| 中超| 大余县| 专栏| 翼城县| 甘孜县| 尤溪县| 峨山| 上栗县| 营山县| 龙海市| 锡林郭勒盟| 达尔| 乌苏市| 阿瓦提县| 共和县| 盈江县| 霍州市| 当雄县| 灵璧县| 松原市| 乌什县| 梅河口市| 习水县| 师宗县| 呼图壁县| 额尔古纳市|