【问题标题】:Service not working properly服务无法正常工作
【发布时间】:2016-02-23 21:06:20
【问题描述】:

我应该在我的服务类中添加一个计时器,它每 10 秒向 LogCat 打印一条消息。一旦我调用 startService 方法,服务类中的任何内容都不会打印,我不知道为什么......有什么想法吗?

package com.murach.reminder;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ReminderActivity extends Activity implements OnClickListener {

private Button startServiceButton;
private Button stopServiceButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_reminder);

    startServiceButton = (Button) findViewById(R.id.startServiceButton);
    stopServiceButton = (Button) findViewById(R.id.stopServiceButton);

    startServiceButton.setOnClickListener(this);
    stopServiceButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    Intent serviceIntent = new Intent(this, ReminderService.class);
    switch (v.getId()) {
        case R.id.startServiceButton:
            // put code to start service and display toast here
            startService(serviceIntent);
            Toast.makeText(this, "Service started", Toast.LENGTH_SHORT).show();
            break;
        case R.id.stopServiceButton:
            // put code to stop service and display toast here
            stopService(serviceIntent);
            Toast.makeText(this, "Service stopped", Toast.LENGTH_SHORT).show();
            break;
    }
}
}

package com.murach.reminder;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import java.util.Timer;
import java.util.TimerTask;

public class ReminderService extends Service
{
private Timer timer;

public void onCreate()
{
    Log.d("Reminder", "Service created");
    startTimer();
}

@Override
public IBinder onBind(Intent intent)
{
    Log.d("Reminder", "No binding for this activity");
    return null;
}

public void onDestroy()
{
    Log.d("Reminder", "Service destroyed");
    stopTimer();
}

private void startTimer() {
    TimerTask task = new TimerTask() {

        public void run() {
            Log.d("Reminder", "Timer task executed");
        }
    };

    timer = new Timer(true);
    int delay = 1000 * 10;
    int interval = 1000 * 10;
    timer.schedule(task, delay, interval);
}

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

这是我在 Manifest 中注册服务的方式(在服务元素中,它不会让我完全输入)

android:name="com.murach.reminder.ReminderService"

【问题讨论】:

    标签: android android-intent android-service logcat


    【解决方案1】:

    package 名称不匹配。您的 Manifest 声明中的单词 murach 存在拼写错误。

    【讨论】:

    • 哦,哎呀。我解决了这个问题,但我仍然遇到同样的问题。日志中没有任何内容
    猜你喜欢
    • 2015-03-06
    • 1970-01-01
    • 2018-01-08
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多