I want to implement a service that handle screen on/off. To do so, I have created BootReceiver
that is called at boot completed and within I start my service.
However, OnCreate
is never called, why?.
When I print the log of the program I never see the onCreate
's log even though I see the onStartCommand
's log. How is this possible? Please help me to understand this.
This is BootReceiver
that is called in the very beginning:
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);
}
}
This is the 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;
}
}
And the manifest:
<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>
Change:
public void OnCreate(){
super.onCreate();
Log.w("TAG", "ScreenListenerService---OnCreate ");
}
to:
public void onCreate(){
super.onCreate();
Log.w("TAG", "ScreenListenerService---OnCreate ");
}
Java is case sensitive, so OnCreate()
!= onCreate()
wrong spelling in onCreate
. You are using OnCreate instead of onCreate
. To get rid of this kind of mistake it is better to use @override
annotation everytime you override a method
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.