最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android開(kāi)發(fā)入門(mén)之Service用法分析

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

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

關(guān)于Service的講解網(wǎng)上已經(jīng)很多了,這里是關(guān)于自己通過(guò)寫(xiě)代碼Service的一點(diǎn)體會(huì) 還有結(jié)合其他人對(duì)Service的一點(diǎn)總結(jié)

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

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

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

本次代碼分別有MainActivity,Java,MyService.java main.xml這幾個(gè)重要文件 下面通過(guò)這幾個(gè)文件對(duì)Service進(jìn)行理解 見(jiàn)注釋

老規(guī)矩 先開(kāi)始布局 挺簡(jiǎn)單的 就是幾個(gè)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測(cè)試"
 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>

布局效果圖:

開(kāi)始服務(wù)文件, 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)建時(shí)被調(diào)用
 @Override
 public void onCreate()
 {
  Log.i(TAG,"onCreate()......");
   showInfo("onCreate()......");
  super.onCreate();
 }
 //當(dāng)客戶調(diào)用startService()啟動(dòng)Service時(shí)被調(diào)用
 @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中幾個(gè)周期函數(shù) 這個(gè)MyService代表一個(gè)服務(wù),當(dāng)然在這里面我們?cè)诶锩婕邮裁磳?shí)質(zhì)性的東西,例如可以在

Onstart(...)函數(shù)里創(chuàng)建一個(gè)音樂(lè)播放器MediaPlayer 當(dāng)服務(wù)被啟動(dòng)時(shí)播放音樂(lè).....

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

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

服務(wù)就這樣 注冊(cè)成功。光注冊(cè)成功還沒(méi)有完成任務(wù)哦......  還有啟動(dòng)服務(wù),停止服務(wù),綁定服務(wù),解除綁定的服務(wù)

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);
  // 開(kāi)啟服務(wù)
  startButton.setOnClickListener(new OnClickListener()
 {
  @Override
  public void onClick(View v)
  {
   // 啟動(dòng)服務(wù)
   startService(buildIntent());
  }
 });
  //startButton按下后 周期函數(shù)執(zhí)行順序 onCreate()----->onStart()
  /******************************************/
  // 停止服務(wù)
  stopButton.setOnClickListener(new OnClickListener()
 {
  @Override
  public void onClick(View v)
  {
   stopService(buildIntent());
  }
 });
  //stopButton按下后 周期函數(shù)執(zhí)行順序 onDestroy()
   /******************************************/
  // 綁定服務(wù)
  bindButton.setOnClickListener(new OnClickListener()
 {
  @Override
  public void onClick(View v)
  {
   bindService(buildIntent(),conn,Service.BIND_AUTO_CREATE);
   /*第三個(gè)參數(shù):如何創(chuàng)建service 一般是制定綁定時(shí)自動(dòng)創(chuàng)建*/
  }
 });
  // bindButton 被按下后(當(dāng)前服務(wù)已經(jīng)stop掉)周期函數(shù)執(zhí)行順序 onCreate()------->onBind()
   /******************************************/
  // 接觸綁定Service
  unbindButton.setOnClickListener(new OnClickListener()
 {
  @Override
  public void onClick(View v)
  {
   unbindService(conn);
  }
 });
  //unbindButton按下后 周期函數(shù)執(zhí)行順序onUnbind()------->onDestroy()
 }
 // 連接對(duì)象
 private ServiceConnection conn = new ServiceConnection()
 {
 @Override
 public void onServiceConnected(ComponentName name, IBinder service)
 {
   Log.i("SERVICE","連接服務(wù)成功!");
   showInfo("連接服務(wù)成功!");
 }
 @Override
 public void onServiceDisconnected(ComponentName name)
 {
  Log.i("SERVICE","服務(wù)連接斷開(kāi)!");
  showInfo("服務(wù)連接斷開(kāi)!");
 }
 };
 public Intent buildIntent()
 {
  Intent intent = new Intent();
  // 設(shè)置Intent屬性 注意:這里action屬性要與Manifest里服務(wù)對(duì)應(yīng)
  intent.setAction(ACTION);
  return intent;
 }
 public void showInfo(String str)
 {
   Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
 }
}

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

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

相關(guān)文章

最新評(píng)論

侯马市| 汉寿县| 桃源县| 东乡| 奎屯市| 托克逊县| 舒城县| 抚顺县| 勐海县| 子长县| 武城县| 临洮县| 马山县| 米泉市| 云和县| 宣武区| 关岭| 北碚区| 兴山县| 西林县| 集贤县| 盘山县| 松桃| 汉沽区| 镇原县| 连江县| 大方县| 榆树市| 呈贡县| 伊宁县| 加查县| 德钦县| 衡水市| 武强县| 双牌县| 蒙山县| 昭平县| 隆林| 集安市| 教育| 合阳县|