【问题标题】:Why does my service calls its onCreate() itself while running?为什么我的服务在运行时会自行调用 onCreate()?
【发布时间】:2013-05-10 08:22:58
【问题描述】:

我的 android 应用程序中有一个服务,该服务仅在退出应用程序时停止。

public class MyService extends Service {

    public MyService() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        try {
            if(intent != null){
                //......
            }
        } catch (Throwable e) {
        }
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //......
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


}

此服务有时会在运行时自行调用其 onCreate() 方法。当检查它正在运行的服务状态时,但其功能不起作用。我被困在这个问题上好几个小时了。这里有什么问题?我试过 onLowMemory () 在此服务中。但不是结果。

【问题讨论】:

    标签: android android-service oncreate


    【解决方案1】:

    如果您在onStartCommand() 中返回START_STICKY,则如果服务被系统杀死(可能在内存不足的情况下),则会再次创建服务。你的情况一定也发生了同样的事情,onCreate() 被调用了。

    【讨论】:

    • 我将 onLowMemory() 覆盖到我的服务并检查了 .但是这个方法没有被调用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-19
    • 2015-07-29
    • 1970-01-01
    • 2023-01-28
    • 1970-01-01
    相关资源
    最近更新 更多