我想實現一個處理屏幕開/關的服務。爲此,我創建了BootReceiver
,在啓動完成時以及在我開始我的服務時調用。onCreate()in service never called
但是,OnCreate
永遠不會被調用,爲什麼?
當我打印程序的日誌時,即使看到onStartCommand
的日誌,我也從不會看到onCreate
的日誌。這怎麼可能?請幫我理解這一點。
這是BootReceiver
,被稱作最開始:
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.w("TAG", "BootReceiver");
Intent service = new Intent(context, ScreenListenerService.class);
context.startService(service);
}
}
這是服務:
public class ScreenListenerService extends Service {
public void OnCreate(){
super.onCreate();
Log.w("TAG", "ScreenListenerService---OnCreate ");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.w("TAG", "ScreenListenerService---onStart ");
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
而且清單:
<service android:name=".ScreenListenerService"></service>
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
</intent-filter>
</receiver>
使用'@ Override'註解抓這些東西,因爲你有'onStartCommand()'和'onBind()'。 – CommonsWare 2013-04-28 17:03:40