Service是android四大組件中與Activity最相似的組件,都可以代表可執行檔程式。
Service與Activity的區別在於: (1)、Service一直在後台運行,沒有使用者介面。 (2)、一旦service被啟動之後,就跟Activity一樣。有自己的生命週期。所以可以沒有Activity。
開發service需要兩個步驟: (1)、定義一個繼承service的子類 (2)、在AndroidManifest.xml中配置該Service ,其過程和配置Activity一樣。
Service運行有兩種方式: 【1】、通過Context的startService()方法,通過該方法啟動用Service,訪問者與service之間沒有關聯,即使 訪問者退出了,Service仍然運行。
【2】、通過Context的bingSerive()方法,使用該方法啟用Service,訪問者和service形成關聯,即綁定在一 起,訪問退出,Service也退出。 而Broadcast Reciver本質是一種全域的監聽器,它可以用來組件之間相互連信。它用來接收程式所發出的Broadcast intent,與應用啟動Activity,service相同的是:程式啟動Broadcast
Reciver也是需要兩個步驟 【1】、建立Broadcast Reciver的Intent 【2】、調用context的sendBroadcase()或者sendorderBroadcase()方法來啟動制定的BroadcastReciver 在筆者下面所示範的代碼中,將這個service和Broadcast Reciver結合在一起,可以不需要activity。當程式接收一個Broadcast Reciver的時候,就啟動service(service也可以通過activity來啟動)。這個例子就是開機自己啟動服務。開機的時候會進行廣播,我們就將這個廣播進行接收,然後開啟服務!Broadcast程式碼:
package com.keenhi.tianpei;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class test_chargeReceive extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent intent1 = new Intent(context ,test_chargeService.class);
context.startService(intent1);
}
}
Service程式碼:package com.keenhi.tianpei;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class test_chargeService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}@Override
public void onCreate()
{ System.out.println("service create");}
}
Manifest.xml程式碼:<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.keenhi.tianpei"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Test_chageActivity"
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="test_chargeService"></service>
<receiver android:name="test_chargeReceive">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
完成上面的程式就ok了,如果你是直接把xxx.apk 用adb push進system/app 這絕對是沒問題的,程式會自啟動。但是如果在eclipse做測試,我發現如果不啟動一次activity的話,BroadcastReceiver 接收不到自啟action,原因不清楚,估計是android為了防止木馬後台運行而又從來沒有提示過使用者吧。無用的Activity程式碼:
package com.keenhi.tianpei;
import android.app.Activity;
import android.os.Bundle;
public class Test_chageActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
大家可以看出,這個activity是絕對沒有用的,只會在eclipse載入程式進機台的時候開啟一次,以後你經過無數次關機,他再不會調用,但service會通過BroadcastReceiver起來。不過測試的時候一定要有activity,切記。
測試好了後的xxx.apk我們可以把activity去掉,直接push進system/app程式完好後台運行。因為有service所以我們可以通過進程式控制制器(設定/應用程式),看到後台啟動並執行進程和服務。但是其實我們可以不要service,只有一個BroadcastReceiver就可以了。但這時請注意,此時沒有service和進程,我們在進程式控制制器是查不到了,但它確實運行了。它會把自己做為系統程式的一部分載入。
測試的時候仍然需要建立一個activity,push進system/app則只需要一個BroadcastReceiver就足夠了。一個BroadcastReceiver的程式和上面一樣,直接把service去掉就可以了。
另外一般這樣的程式都會做成和系統一樣的守護進程,讓它不能被kill掉。方法如下:在manifest.xml檔案下添加:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:sharedUserId="android.uid.system">
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:allowClearUserData="false" android:process="system"
android:killAfterRestore="false">