服务中的onCreate()从未被调用过

Joh*_*ith 0 android android-service

我想实现一个处理屏幕开/关的服务.为此,我创建BootReceiver了在启动时调用并在我启动服务的过程中调用.

但是,OnCreate从来没有被称为,为什么?

当我打印程序onCreate的日志时,即使看到日志,我也看不到onStartCommand日志.这怎么可能?请帮我理解这个.

这是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);

    }

}
Run Code Online (Sandbox Code Playgroud)

这是服务:

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

并且清单:

<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>
Run Code Online (Sandbox Code Playgroud)

Rag*_*ood 6

更改:

public void OnCreate(){
    super.onCreate();
    Log.w("TAG", "ScreenListenerService---OnCreate ");
}
Run Code Online (Sandbox Code Playgroud)

至:

public void onCreate(){
    super.onCreate();
    Log.w("TAG", "ScreenListenerService---OnCreate ");
}
Run Code Online (Sandbox Code Playgroud)

Java区分大小写,所以OnCreate()!=onCreate()