導航:首頁 > 操作系統 > androidservices的啟動

androidservices的啟動

發布時間:2022-08-22 18:34:42

android service

Service,看名字就知道跟正常理解的「服務」差不多,後台運行,可交互這樣的一個東西。它跟Activity的級別差不多,但是他不能自己運行,需要通過某一個Activity或者其他Context對象來調用, Context.startService() 和 Context.bindService()。 兩種啟動Service的方式有所不同。這里要說明一下的是如果你在Service的onCreate或者onStart做一些很耗時間的事情,最好在Service里啟動一個線程來完成,因為Service是跑在主線程中,會影響到你的UI操作或者阻塞主線程中的其他事情。 什麼時候需要Service呢?比如播放多媒體的時候用戶啟動了其他Activity這個時候程序要在後台繼續播放,比如檢測SD卡上文件的變化,再或者在後台記錄你地理信息位置的改變等等,總之服務嘛,總是藏在後頭的。

⑵ android上怎樣讓一個Service開機自動啟動

Android開機啟動Service,需要使用BroadcastReceiver,Android系統,開機會發送一個開機廣播,可以通過BroadcastReceiver來接收開機廣播。
具體代碼:
1.在配置文件AndroidManifest.xml中向系統注冊receiver
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>

2.需要添加相應許可權
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

3.在Receiver中就可以添加開機需要進行的操作
public class BootCompletedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

}
}

4.執行開機後的操作,Intent intent = new Intent(context,Service.class); context.startService(intent); 這樣即可開機啟動Service了。

⑶ Android如何實現開機自啟動Service

請問,我使用自動啟動的方式,想debug
Service里的代碼內容,從我的log文件裡面看,Receive已經接收到啟動消息,並啟動了Service,但是程序並沒有停在斷點位置,請問可能是什麼緣故啊?

⑷ Android中怎麼啟動關閉Service及功能解釋

調用startService就是啟動service,調用stopService就是關閉service。

android中Service是運行在後台的東西,級別與activity差不多。既然說service是運行在後台的服務,那麼它就是不可見的,沒有界面的東西。可以啟動一個服務Service來播放音樂,或者記錄地理信息位置的改變,或者啟動一個服務來運行並一直監聽某種動作。Service和其他組件一樣,都是運行在主線程中,因此不能用它來做耗時的請求或者動作。

服務一般分為兩種:
1:本地服務, Local Service 用於應用程序內部。在Service可以調用Context.startService()啟動,調用Context.stopService()結束。在內部可以調用Service.stopSelf() 或 Service.stopSelfResult()來自己停止。無論調用了多少次startService(),都只需調用一次stopService()來停止。
2:遠程服務, Remote Service 用於android系統內部的應用程序之間。可以定義介面並把介面暴露出來,以便其他應用進行操作。客戶端建立到服務對象的連接,並通過那個連接來調用服務。調用Context.bindService()方法建立連接,並啟動,以調用 Context.unbindService()關閉連接。多個客戶端可以綁定至同一個服務。如果服務此時還沒有載入,bindService()會先載入它。

⑸ Android Application 裡面怎麼啟動service

1.
Intent
intent
=
new
Intent(A.this,Service.class);
startService(intent);
在同一個應用任何地方調用
startService()
方法就能啟動
Service
了,然後系統會回調
Service
類的
onCreate()
以及
onStart()
方法。這樣啟動的
Service
會一直運行在後台,直到
Context.stopService()
或者
selfStop()
方法被調用。另外如果一個
Service
已經被啟動,其他代碼再試圖調用
startService()
方法,是不會執行
onCreate()
的,但會重新執行一次
onStart()

2.
Intent
intent
=
new
Intent(A.this,Service.class);
ServiceConnection
conn
=
new
ServiceConnection(){//邏輯操作};
bindService(intent,
conn,
Context.BIND_AUTO_CREATE);
bindService()
方法的意思是,把這個
Service
和調用
Service
的客戶類綁起來,如果調用這個客戶類被銷毀,Service
也會被銷毀。用這個方法的一個好處是,bindService()
方法執行後
Service
會回調上邊提到的
onBind()
方發,你可以從這里返回一個實現了
IBind
介面的類,在客戶端操作這個類就能和這個服務通信了,比如得到
Service
運行的狀態或其他操作。如果
Service
還沒有運行,使用這個方法啟動
Service
就會
onCreate()
方法而不會調用
onStart()。
區別概況為:
startService()
的調用者與服務沒有聯系,即使調用者退出了,服務仍然運行,而bindService()
的調用者與服務綁在一起,調用者一旦退出了,服務也隨即終止掉。

⑹ 怎麼開發android service 開機啟動

第一步:首先創建一個廣播接收者,重構其抽象方法 onReceive(Context context, Intent intent),在其中啟動你想要啟動的Service或app。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class BootBroadcastReceiver extends BroadcastReceiver {
//重寫onReceive方法
@Override
public void onReceive(Context context, Intent intent) {
//後邊的XXX.class就是要啟動的服務
Intent service = new Intent(context,XXXclass);
context.startService(service);
Log.v("TAG", "開機自動服務自動啟動.....");
//啟動應用,參數為需要自動啟動的應用的包名
Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
context.startActivity(intent );
}

}
第二步:配置xml文件,在receiver接收這種添加intent-filter配置
<receiver android:name="BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
第三步:添加許可權 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

⑺ 原創:Android怎麼讓一個service開機自動啟動

Android開機啟動Service,需要使用BroadcastReceiver,Android系統,開機會發送一個開機廣播,可以通過BroadcastReceiver來接收開機廣播。
具體代碼:
1.在配置文件AndroidManifest.xml中向系統注冊receiver
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>

2.需要添加相應許可權
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

3.在Receiver中就可以添加開機需要進行的操作
public class BootCompletedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

}
}

4.執行開機後的操作,Intent intent = new Intent(context,Service.class); context.startService(intent); 這樣即可開機啟動Service了。
分享
本回答由電腦網路分類達人 孟男男認證
2015-02-05 18:47
#芝麻嘉年華,給你不一樣的新春驚喜!#
提問者採納
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<!-- 開機啟動 -->
<receiver android:name=".BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
public class BootBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

安卓怎麼自動啟動service

1、首先創建一個廣播接收者,重構其抽象方法onReceive(Context context, Intent intent),在其中啟動你想要啟動的Service。import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class BootBroadcastReceiver extends BroadcastReceiver {
//重寫onReceive方法
@Override
public void onReceive(Context context, Intent intent) {
//後邊的XXX.class就是要啟動的服務
Intent service = new Intent(context,XXXclass);
context.startService(service);
Log.v("TAG", "開機自動服務自動啟動.....");

}

}
配置xml文件,在receiver接收這種添加intent-filter配置
<receiver android:name="BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
添加許可權處理
<!--此許可權在高版本中可以省略,你可以測試-->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
創建你需要啟動的service處理(的XXX)類,並在androidMainfest.xml文件中添加(XXX)service的配置即可。

⑼ android中當Service在運行時怎麼重啟Service

只需要重新startService即可重新調用service的onStart()。

  1. Service是在一段不定的時間運行在後台,不和用戶交互應用組件。每個Service必須在manifest中 通過<service>來聲明。可以通過contect.startservice和contect.bindserverice來啟動。

  2. Service和其他的應用組件一樣,運行在進程的主線程中。這就是說如果service需要很多耗時或者阻塞的操作,需要在其子線程中實現。


service的兩種模式(startService()/bindService() :

1.本地服務 Local Service 用於應用程序內部。

它可以啟動並運行,直至有人停止了它或它自己停止。在這種方式下,它以調用Context.startService()啟動,而以調用Context.stopService()結束。它可以調用Service.stopSelf() 或 Service.stopSelfResult()來自己停止。不論調用了多少次startService()方法,你只需要調用一次stopService()來停止服務。

用於實現應用程序自己的一些耗時任務,比如查詢升級信息,並不佔用應用程序比如Activity所屬線程,而是單開線程後台執行,這樣用戶體驗比較好。


2.遠程服務 Remote Service 用於android系統內部的應用程序之間。

它可以通過自己定義並暴露出來的介面進行程序操作。客戶端建立一個到服務對象的連接,並通過那個連接來調用服務。連接以調用Context.bindService()方法建立,以調用 Context.unbindService()關閉。多個客戶端可以綁定至同一個服務。如果服務此時還沒有載入,bindService()會先載入它。

⑽ Android如何啟用Service,如何停用Service。

1.第一種是通過調用Context.startService()啟動,調用Context.stopService()結束,startService()可以傳遞參數給Service
2.第二種方式是通過調用Context.bindService()啟動,調用Context.unbindservice()結束,還可以通過ServiceConnection訪問Service。
在Service每一次的開啟關閉過程中,只有onStart可被多次調用(通過多次startService調用),其他onCreate,onBind,onUnbind,onDestory在一個生命周期中只能被調用一次。

閱讀全文

與androidservices的啟動相關的資料

熱點內容
pdfa5 瀏覽:936
多大的程序員吃香 瀏覽:896
編程字體調節 瀏覽:930
水準儀移動點位演算法視頻 瀏覽:68
單片機模擬怎麼顯示3s的方波 瀏覽:930
小型企業文本檢索php源碼 瀏覽:232
聯想伺服器主板損壞怎麼恢復數據 瀏覽:507
定製伺服器怎麼做 瀏覽:650
2021榮放怎麼下載app 瀏覽:206
命令關閉hyperv 瀏覽:147
重啟sftp服務的命令 瀏覽:38
愛情自有天意電影版 瀏覽:34
dns伺服器地址在哪裡獲取 瀏覽:552
海航的伺服器地址 瀏覽:436
蘋果手機怎麼把本地音樂導入app 瀏覽:617
黑暗與光明控制台命令 瀏覽:219
linux伺服器巡檢 瀏覽:684
安卓qq分享屏幕沒聲音怎麼辦 瀏覽:708
空餘時間編程實例 瀏覽:441
430單片機模塊 瀏覽:1008