2013-04-28 85 views
0

我想實現一個處理屏幕開/關的服務。爲此,我創建了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> 
+0

使用'@ Override'註解抓這些東西,因爲你有'onStartCommand()'和'onBind()'。 – CommonsWare 2013-04-28 17:03:40

回答

6

變化:

public void OnCreate(){ 
    super.onCreate(); 
    Log.w("TAG", "ScreenListenerService---OnCreate "); 
} 

到:

public void onCreate(){ 
    super.onCreate(); 
    Log.w("TAG", "ScreenListenerService---OnCreate "); 
} 

Java是大小寫敏感的,所以OnCreate() = onCreate()

+0

非常感謝。 – 2013-04-28 17:03:42

+0

你能檢查我的其他問題嗎? http://stackoverflow.com/questions/16265667/android-broadcastreceiver-is-never-called – 2013-04-28 17:43:25

0

錯誤拼寫onCreate!您正在使用OnCreate而不是onCreate。爲了擺脫這種錯誤的,最好是用@override註釋每次您覆蓋方法

相關問題