的onCreate()的服务从来没有所谓的 [英] onCreate() in service never called

查看:146
本文介绍了的onCreate()的服务从来没有所谓的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要实现开/关处理屏幕的服务。要做到这一点,我已经创建 BootReceiver 被称为在引导完成,在我开始我的服务。

然而,的OnCreate 永远不会被调用,这是为什么?

当我打印程序我从来没有看到的onCreate 的日志记录,即使我看到 onStartCommand 的日志。这怎么可能?请帮我理解这一点。

这是 BootReceiver 被称为最开始:

 公共类BootReceiver扩展广播接收器{    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){        Log.w(TAG,BootReceiver);        意向书的服务=新意图(背景下,ScreenListenerService.class);
            context.startService(服务);    }}

这是该服务:

 公共类ScreenListenerService延伸服务{
    公共无效的onCreate(){
        super.onCreate();
        Log.w(TAG,ScreenListenerService ---在OnCreate);
    }
     @覆盖
     公众诠释onStartCommand(意向意图,诠释标志诠释startId){            Log.w(TAG,ScreenListenerService ---在onStart);
        }    @覆盖
    公众的IBinder onBind(意向意图){
        // TODO自动生成方法存根
        返回null;
    }}

和清单:

 <服务机器人:名字=。ScreenListenerService>< /服务>        <接收机器人:名字=。BootReceiver>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.BOOT_COMPLETED/>
                 <作用机器人:名字=android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE/>
            &所述; /意图滤光器>
        < /接收器>


解决方案

修改

 公共无效的onCreate(){
    super.onCreate();
    Log.w(TAG,ScreenListenerService ---在OnCreate);
}

 公共无效的onCreate(){
    super.onCreate();
    Log.w(TAG,ScreenListenerService ---在OnCreate);
}

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

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()

这篇关于的onCreate()的服务从来没有所谓的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆