为什么我的服务在 Android 中不起作用?(我只想每 5 秒记录一次) [英] Why doesn't my Service work in Android? (I just want to log something ever 5 seconds)

查看:16
本文介绍了为什么我的服务在 Android 中不起作用?(我只想每 5 秒记录一次)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个名为 HelloService 的新类.我将此添加到 Android manifest.xml.

I created a new class called HelloService. I added this to the Android manifest.xml.

public class HelloService extends Service {
    private Timer timer = new Timer();
    private long INTERVAL = 5000;

    public void onCreate() {
        super.onCreate();
        startservice();

    }

    private void startservice() {
        timer.scheduleAtFixedRate( new TimerTask() {
            public void run() {
                Log.d("servy", "This proves that my service works.");
            }
        }, 0, INTERVAL);
    ; }

    private void stopservice() {
        if (timer != null){
            timer.cancel();
        }
    }

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

我的其他活动是这样调用的:

My other activity calls it like this:

    Intent helloservice = new Intent(this, HelloService.class);
    startService(helloservice);

出于某种原因,我在我的新 HelloService 中放置了一个断点……但它甚至没有命中.它也没有记录.

For some reason, I put a breakpoint in my new HelloService...but it's not even hitting. It's not logging either.

无法启动服务 Intent { cmp = com.examples.hello/.HelloService }:未找到"

这是什么意思?...我在与其他所有内容相同的地方创建了 HelloService.java...

What does that mean? ... I created HelloService.java in the same place as everything else...

解决了.我修复了我的清单文件.感谢尼古拉斯米利亚尼奇

Solved. I fixed my manifest file. Thanks Nikola Smiljanic

<service android:name=".HelloService"/>

到:

   <service android:name="HelloService"></service>

推荐答案

也许您没有在清单中声明服务.无论如何,您使用的是错误的课程.您必须使用 AlarmManager 来编程事件.看到那个链接,它对我很有用.

Maybe you're not declaring the service in your manifiest. Anyway you're using the wrong class. You have to use AlarmManager for programing events. See that link, it was very useful to me.

使用alarmManager和service执行调度仅在特定时间段内通知

这篇关于为什么我的服务在 Android 中不起作用?(我只想每 5 秒记录一次)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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