只有一個Service或Broadcast Reciver的android應用


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 {
   
    @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">

 

 

轉自:http://blog.sina.com.cn/s/blog_76550fd701017qej.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM