亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android開發(fā)入門之Service用法分析

 更新時間:2016年07月08日 15:35:56   作者:manymore13  
這篇文章主要介紹了Android中Service用法,較為詳細的分析了Service的功能、相關函數(shù)使用及注意事項,需要的朋友可以參考下

本文實例講述了Android中Service用法。分享給大家供大家參考,具體如下:

關于Service的講解網(wǎng)上已經很多了,這里是關于自己通過寫代碼Service的一點體會 還有結合其他人對Service的一點總結

Service可以理解為一個隱形的Activity 但它又與Activity有些不同,首先Service是沒界面,用戶看不到 可交互的組件 級別是與Activity是差不多的

Service中定義了一系列和自身聲明周期相關的方法:

onBind(...)是必須實現(xiàn)的方法,返回一個綁定接口給Service
onCreate(); 當Service第一次被創(chuàng)建時 由系統(tǒng)調用
onStart(...)當通過startService()方法調用啟動Service時被調用
onDestroy();當Service不再使用,系統(tǒng)調用該方法....

本次代碼分別有MainActivity,Java,MyService.java main.xml這幾個重要文件 下面通過這幾個文件對Service進行理解 見注釋

老規(guī)矩 先開始布局 挺簡單的 就是幾個Button

<?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="Service測試"
 android:gravity="center_horizontal"
 />
<Button
 android:id="@+id/startButton"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="Start Service"
/>
<Button
 android:id="@+id/stopButton"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="Stop Service"
/>
<Button
 android:id="@+id/bindButton"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="Bind Service"
/>
<Button
 android:id="@+id/unBindButton"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="Unbind Service"
/>
</LinearLayout>

布局效果圖:

開始服務文件, MyService繼承Service

public class MyService extends Service
{
 final static String TAG = "MyService";
 @Override
 public IBinder onBind(Intent intent)
 {
  Log.i(TAG,"OnBind()......");
  showInfo("Onbind()......");
  return null;
 }
 // Setvice創(chuàng)建時被調用
 @Override
 public void onCreate()
 {
  Log.i(TAG,"onCreate()......");
   showInfo("onCreate()......");
  super.onCreate();
 }
 //當客戶調用startService()啟動Service時被調用
 @Override
 public void onStart(Intent intent, int startId)
 {
  Log.i(TAG,"onStart()........");
  showInfo("onStart()........");
  super.onStart(intent, startId);
 }
 @Override
 public void onDestroy()
 {
  Log.i(TAG,"onDestroy()........");
  showInfo("onDestroy()........");
  super.onDestroy();
 }
 @Override
 public boolean onUnbind(Intent intent)
 {
  Log.i(TAG,"onUnbind()........");
  showInfo("onUnbind()........");
  return super.onUnbind(intent);
 }
 public void showInfo(String str)
 {
  Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
 }
}

上面主要是Service中幾個周期函數(shù) 這個MyService代表一個服務,當然在這里面我們在里面加什么實質性的東西,例如可以在

Onstart(...)函數(shù)里創(chuàng)建一個音樂播放器MediaPlayer 當服務被啟動時播放音樂.....

你創(chuàng)建了Service  就跟你創(chuàng)建Activity一樣 必須在Manifest里注冊 下面開始注冊

<!-- 增加的Service -->
<service android:name=".MyService">
<intent-filter >
 <action android:name="com.study.android.action.MY_SERVICE"/>
</intent-filter>
</service>

服務就這樣 注冊成功。光注冊成功還沒有完成任務哦......  還有啟動服務,停止服務,綁定服務,解除綁定的服務

package com.study.android;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
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 MainActivity extends Activity {
 final static String ACTION = "com.study.android.action.MY_SERVICE";
 private Button startButton,stopButton;
 private Button bindButton,unbindButton;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  startButton = (Button)findViewById(R.id.startButton);
  stopButton = (Button)findViewById(R.id.stopButton);
  bindButton = (Button)findViewById(R.id.bindButton);
  unbindButton = (Button)findViewById(R.id.unBindButton);
  // 開啟服務
  startButton.setOnClickListener(new OnClickListener()
 {
  @Override
  public void onClick(View v)
  {
   // 啟動服務
   startService(buildIntent());
  }
 });
  //startButton按下后 周期函數(shù)執(zhí)行順序 onCreate()----->onStart()
  /******************************************/
  // 停止服務
  stopButton.setOnClickListener(new OnClickListener()
 {
  @Override
  public void onClick(View v)
  {
   stopService(buildIntent());
  }
 });
  //stopButton按下后 周期函數(shù)執(zhí)行順序 onDestroy()
   /******************************************/
  // 綁定服務
  bindButton.setOnClickListener(new OnClickListener()
 {
  @Override
  public void onClick(View v)
  {
   bindService(buildIntent(),conn,Service.BIND_AUTO_CREATE);
   /*第三個參數(shù):如何創(chuàng)建service 一般是制定綁定時自動創(chuàng)建*/
  }
 });
  // bindButton 被按下后(當前服務已經stop掉)周期函數(shù)執(zhí)行順序 onCreate()------->onBind()
   /******************************************/
  // 接觸綁定Service
  unbindButton.setOnClickListener(new OnClickListener()
 {
  @Override
  public void onClick(View v)
  {
   unbindService(conn);
  }
 });
  //unbindButton按下后 周期函數(shù)執(zhí)行順序onUnbind()------->onDestroy()
 }
 // 連接對象
 private ServiceConnection conn = new ServiceConnection()
 {
 @Override
 public void onServiceConnected(ComponentName name, IBinder service)
 {
   Log.i("SERVICE","連接服務成功!");
   showInfo("連接服務成功!");
 }
 @Override
 public void onServiceDisconnected(ComponentName name)
 {
  Log.i("SERVICE","服務連接斷開!");
  showInfo("服務連接斷開!");
 }
 };
 public Intent buildIntent()
 {
  Intent intent = new Intent();
  // 設置Intent屬性 注意:這里action屬性要與Manifest里服務對應
  intent.setAction(ACTION);
  return intent;
 }
 public void showInfo(String str)
 {
   Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
 }
}

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android Service組件使用技巧總結》、《Android編程之activity操作技巧總結》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android操作SQLite數(shù)據(jù)庫技巧總結》、《Android操作json格式數(shù)據(jù)技巧總結》、《Android數(shù)據(jù)庫操作技巧總結》、《Android開發(fā)入門與進階教程》、《Android視圖View技巧總結》及《Android控件用法總結

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

相關文章

  • Android開發(fā)Jetpack組件DataBinding用例詳解

    Android開發(fā)Jetpack組件DataBinding用例詳解

    這篇文章主要為大家介紹了Android開發(fā)Jetpack組件DataBinding的使案用例詳解說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2022-02-02
  • Android搭建本地Tomcat服務器及相關配置

    Android搭建本地Tomcat服務器及相關配置

    這篇文章主要介紹了Android搭建本地Tomcat服務器及相關配置,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Android開發(fā)基礎實現(xiàn)最簡單的視頻播放示例

    Android開發(fā)基礎實現(xiàn)最簡單的視頻播放示例

    這篇文章主要為大家介紹了Android開發(fā)基礎實現(xiàn)最簡單的視頻播放示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • Android通過overScrollBy實現(xiàn)下拉視差特效

    Android通過overScrollBy實現(xiàn)下拉視差特效

    這篇文章主要為大家詳細介紹了Android通過overScrollBy實現(xiàn)下拉視差特效,實現(xiàn)精彩的阻尼效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android組件Glide實現(xiàn)圖片平滑滾動效果

    Android組件Glide實現(xiàn)圖片平滑滾動效果

    這篇文章主要介紹了Android組件Glide實現(xiàn)圖片平滑滾動效果的相關資料,具有一定的參考價值,需要的朋友可以參考下
    2016-07-07
  • Android ImageLoader第三方框架解析

    Android ImageLoader第三方框架解析

    這篇文章主要為大家詳細解析了Android ImageLoader第三方框架,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Flutter實現(xiàn)簡單的內容高亮效果

    Flutter實現(xiàn)簡單的內容高亮效果

    內容高亮并不陌生,特別是在搜索內容頁面,可以說四處可見,這篇文章主要為大家介紹了如何使用Flutter實現(xiàn)簡單的內容高亮效果,需要的可以參考下
    2023-08-08
  • android控件實現(xiàn)單擊拖動效果

    android控件實現(xiàn)單擊拖動效果

    這篇文章主要為大家詳細介紹了android控件實現(xiàn)單擊拖動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • Android?CameraX?打開攝像頭預覽功能

    Android?CameraX?打開攝像頭預覽功能

    這篇文章主要介紹了Android?CameraX?打開攝像頭預覽功能,模塊gradle的一些配置,使用的Android?SDK版本為31,啟用了databinding,具體實例代碼跟隨小編一起看看吧
    2021-12-12
  • Android實現(xiàn)省市區(qū)三級聯(lián)動

    Android實現(xiàn)省市區(qū)三級聯(lián)動

    這篇文章主要為大家詳細介紹了Android實現(xiàn)省市區(qū)三級聯(lián)動,Spinner實現(xiàn)省市區(qū)的三級聯(lián)動,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07

最新評論