android教程之service使用方法示例詳解
Service的生命周期 (適用于2.1及以上)
1. 被startService的
無論是否有任何活動綁定到該Service,都在后臺運(yùn)行。onCreate(若需要) -> onStart(int id, Bundle args). 多次startService,則onStart調(diào)用多次,但不會創(chuàng)建多個Service實(shí)例,只需要一次stop。該Service一直后臺運(yùn)行,直到stopService或者自己的stopSelf()或者資源不足由平臺結(jié)束。
2. 被bindService的
調(diào)用bindService綁定,連接建立服務(wù)一直運(yùn)行。未被startService只是BindService,則onCreate()執(zhí)行,onStart(int,Bundle)不被調(diào)用;這種情況下綁定被解除,平臺就可以清除該Service(連接銷毀后,會導(dǎo)致解除,解除后就會銷毀)。
3. 被啟動又被綁定
類似startService的生命周期,onCreate onStart都會調(diào)用。
4. 停止服務(wù)時
stopService時顯式onDestroy()?;虿辉儆薪壎?沒有啟動時)時隱式調(diào)用。有bind情況下stopService()不起作用。
以下是一個簡單的實(shí)現(xiàn)例子,某些部分需要配合logcat觀察。
AcMain.java
package jtapp.myservicesamples;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
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 AcMain extends Activity implements OnClickListener {
private static final String TAG = "AcMain";
private Button btnStart;
private Button btnStop;
private Button btnBind;
private Button btnExit;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findView();
}
private void findView() {
btnStart = (Button) findViewById(R.id.Start);
btnStop = (Button) findViewById(R.id.Stop);
btnBind = (Button) findViewById(R.id.Bind);
btnExit = (Button) findViewById(R.id.Exit);
btnStart.setOnClickListener(this);
btnStop.setOnClickListener(this);
btnBind.setOnClickListener(this);
btnExit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent("jtapp.myservicesamples.myservice");
switch(v.getId()) {
case R.id.Start:
startService(intent);
Toast.makeText(this,
"myservice running " + MyService.msec/1000.0 + "s.",
Toast.LENGTH_LONG).show();
break;
case R.id.Stop:
stopService(intent);
Toast.makeText(this,
"myservice running " + MyService.msec/1000.0 + "s.",
Toast.LENGTH_LONG).show();
break;
case R.id.Bind:
bindService(intent, sc, Context.BIND_AUTO_CREATE);
break;
case R.id.Exit:
this.finish();
break;
}
}
private MyService serviceBinder;
private ServiceConnection sc = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "in onServiceDisconnected");
serviceBinder = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "in onServiceConnected");
serviceBinder = ((MyService.MyBinder)service).getService();
}
};
@Override
protected void onDestroy() {
//this.unbindService(sc);
//this.stopService(
// new Intent("jtapp.myservicesamples.myservice"));
super.onDestroy();
}
}
main.xml
<?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="@string/hello" />
<Button android:text="Start MyService" android:id="@+id/Start"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<Button android:text="Stop MyService" android:id="@+id/Stop"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<Button android:text="Bind MyService" android:id="@+id/Bind"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<Button android:text="Exit AcMain" android:id="@+id/Exit"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
MyService.java
package jtapp.myservicesamples;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private static final String TAG = "MyService";
public static long msec = 0;
private boolean bThreadRunning = true;
private final IBinder binder = new MyBinder();
public class MyBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
@Override
public void onCreate() {
new Thread(new Runnable(){
@Override
public void run() {
while (bThreadRunning) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
Log.i(TAG, "myservice running " + (msec+=100) + "ms.");
}
}
}).start();
}
@Override
public void onDestroy() {
bThreadRunning = false;
super.onDestroy(); // 可以不用
}
}
AnndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jtapp.myservicesamples" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:debuggable="true">
<activity android:name=".AcMain" 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">
<intent-filter>
<action android:name="jtapp.myservicesamples.myservice"></action>
</intent-filter>
</service>
</application>
</manifest>
- Android基于Service的音樂播放器
- Android通過startService播放背景音樂
- Android中實(shí)現(xiàn)開機(jī)自動啟動服務(wù)(service)實(shí)例
- Android中的Service相關(guān)全面總結(jié)
- Android應(yīng)用程序四大組件之使用AIDL如何實(shí)現(xiàn)跨進(jìn)程調(diào)用Service
- android使用Messenger綁定Service的多種實(shí)現(xiàn)方法
- android調(diào)用web service(cxf)實(shí)例應(yīng)用詳解
- Android創(chuàng)建服務(wù)之started service詳細(xì)介紹
- 基于Android Service 生命周期的詳細(xì)介紹
- Android基于service實(shí)現(xiàn)音樂的后臺播放功能示例
相關(guān)文章
基于Android中的 AutoCompleteTextView實(shí)現(xiàn)自動填充
本篇文章小編為大家介紹,基于Android中的 AutoCompleteTextView實(shí)現(xiàn)自動填充。需要的朋友參考下2013-04-04
基于Android實(shí)現(xiàn)滾動頭部懸停效果
這篇文章主要為大家詳細(xì)介紹了如何在?Android?中實(shí)現(xiàn)這種滾動頭部懸停效果,并提供完整源碼,方便學(xué)習(xí)和實(shí)際應(yīng)用,有需要的小伙伴可以了解一下2025-04-04
Android 根據(jù)手勢頂部View自動展示與隱藏效果
這篇文章主要介紹了Android 根據(jù)手勢頂部View自動展示與隱藏效果,本文給大家介紹非常詳細(xì)包括實(shí)現(xiàn)原理和實(shí)例代碼,需要的朋友參考下吧2017-08-08
Android程序開發(fā)之ListView實(shí)現(xiàn)橫向滾動(帶表頭與固定列)
這篇文章主要介紹了Android程序開發(fā)之ListView實(shí)現(xiàn)橫向滾動(帶表頭與固定列)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07
Android Studio 自定義Debug變量視圖的方法
這篇文章主要介紹了Android Studio 自定義Debug變量視圖的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
Android仿網(wǎng)易客戶端頂部導(dǎo)航欄效果
這篇文章主要為大家詳細(xì)介紹了Android仿網(wǎng)易客戶端頂部導(dǎo)航欄效果,幫助大家制作網(wǎng)易客戶端導(dǎo)航欄特效,感興趣的小伙伴們可以參考一下2016-06-06
android使用PullToRefresh實(shí)現(xiàn)下拉刷新和上拉加載

