Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 后台服务不支持';不发起_Android_Background Process - Fatal编程技术网

Android 后台服务不支持';不发起

Android 后台服务不支持';不发起,android,background-process,Android,Background Process,我正在尝试启动一个后台服务,但它没有运行。以下是服务代码: import android.app.Service; import android.content.Intent; import android.os.Handler; import android.os.IBinder; public class BackgroundData extends Service { private final Handler handler = new Handler(); Inte

我正在尝试启动一个
后台服务
,但它没有运行。以下是服务代码:

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;

public class BackgroundData extends Service {

    private final Handler handler = new Handler();
    Intent intent;
    int time;

    @Override
    public void onCreate() {
        // Called on service created

        time = 1000;
    }

    @Override
    public void onStart(Intent intent, int startid) {

        handler.removeCallbacks(sendUpdatesToUI);
        handler.postDelayed(sendUpdatesToUI, time); // 1 sec
    }

     private Runnable sendUpdatesToUI = new Runnable() {
            public void run() {

                System.out.println("OMEGAN...");

                handler.postDelayed(this, time); // 1 sec
            }
        };

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
下面是我如何调用服务启动的:

startService(new Intent(this, BackgroundData.class));
问题是什么?此代码来自此处:

我已经更改了它,以便按照我的需要使用它,并且它可以工作,但是当我将它实现到我的代码中时,它就不工作了

有什么想法吗


提前谢谢你

好的,我发现了我的问题。我声明了
.BackgroundData
,但是这个文件放在了另一个包中


我解决了将
.BackgroundData
更改为
my.own.package.BackgroundData

启动服务时,日志中是否出现任何错误?您确定您的服务没有运行吗?尝试将日志语句作为onCreate()中的第一行进行验证。另外,从活动中调用startService()的位置在哪里?广播接收机?如果是后者,可能服务还可以,但接收者接收不正确。嗨@Tim。我没有在logcat中得到任何错误,我几乎可以肯定服务不会启动,因为我尝试了您告诉我的内容和
System.out.println(“某物”)未显示在日志中。。。我正在从一个活动调用startService。你在清单文件中声明了该服务吗?@TobiasMoeThorstensen是的,我是这样做的
@KrLx\u roller
System.out.pringln()
在Android中不起任何作用。LogCat不是一个控制台。相反,您应该使用Android
log
类生成日志消息。