Android開(kāi)發(fā)入門(mén)之Service用法分析
本文實(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)文章
Android使用xml文件資源定義菜單實(shí)現(xiàn)方法示例
這篇文章主要介紹了Android使用xml文件資源定義菜單實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android資源文件管理及xml配置自定義菜單相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
使用Android Studio檢測(cè)內(nèi)存泄露(LeakCanary)
本篇文章主要介紹了用Android Studio檢測(cè)內(nèi)存泄露的問(wèn)題的解決方法,Android Studio在為我們提供了良好的編碼體驗(yàn)的同時(shí),也提供了許多對(duì)App性能分析的工具,下面我們一起來(lái)了解一下。2016-12-12
Android之ArcSlidingHelper制作圓弧滑動(dòng)效果
這篇文章主要介紹了Android之ArcSlidingHelper制作圓弧滑動(dòng)效果,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
Android 個(gè)人理財(cái)工具二:使用SQLite實(shí)現(xiàn)啟動(dòng)時(shí)初始化數(shù)據(jù)
本文主要介紹 Android 使用SQLite實(shí)現(xiàn)啟動(dòng)時(shí)初始化數(shù)據(jù),這里對(duì)SQLite 的數(shù)據(jù)庫(kù)進(jìn)行詳解,附有示例代碼,有興趣的小伙伴可以參考下2016-08-08
Android網(wǎng)格布局GridView實(shí)現(xiàn)漂亮的多選效果
這篇文章主要為大家詳細(xì)介紹了Android網(wǎng)格布局GridView實(shí)現(xiàn)漂亮的多選效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android使用CoordinatorLayout實(shí)現(xiàn)底部彈出菜單
這篇文章主要為大家詳細(xì)介紹了Android使用CoordinatorLayout實(shí)現(xiàn)底部彈出菜單,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
Android開(kāi)發(fā)實(shí)現(xiàn)的計(jì)時(shí)器功能示例
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)的計(jì)時(shí)器功能,涉及Android開(kāi)發(fā)中的計(jì)時(shí)器相關(guān)組件布局、調(diào)用、事件響應(yīng)等相關(guān)操作技巧,需要的朋友可以參考下2019-04-04
android studio節(jié)省C盤(pán)空間的配置方法
這篇文章主要介紹了android studio節(jié)省C盤(pán)空間的配置方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-07-07

