一个服务中的Andr​​oid计时器 [英] Android Timer within a service

查看:175
本文介绍了一个服务中的Andr​​oid计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有运行在我创建了一个服务的计时器问题。计时器调用简单的任务不叫。我知道,在服务启动,因为我已经把在它的祝酒词,他们是所谓的,但不是当他们在定时器中。帮助AP preciated。

服务类:

 公共类本地服务延伸服务
{
    私有静态定时器定时=新的Timer();
    私人上下文CTX;

    公众的IBinder onBind(意向为arg0)
    {
          返回null;
    }

    公共无效的onCreate()
    {
          super.onCreate();
          CTX =这一点;
          startService();
    }

    私人无效startService()
    {
        timer.scheduleAtFixedRate(新MainTask的(),0,5000);
    }

    私有类MainTask的扩展的TimerTask
    {
        公共无效的run()
        {
            Toast.makeText(CTX,测试,Toast.LENGTH_SHORT).show();
        }
    }

    公共无效的onDestroy()
    {
          super.onDestroy();
          Toast.makeText(本,服务已停止......,Toast.LENGTH_SHORT).show();
    }
}
 

主要类:

 公共无效的onCreate(包savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    startService(新意图(RingerSchedule.this,LocalService.class));
}
 

解决方案

安卓不允许像敬酒UI事件从主线程之外。该运行获取调用,但面包被忽略。

要创建UI线程上的面包,你可以使用一个处理程序和像这样的空消息:

 公共类本地服务延伸服务
{
    私有静态定时器定时=新的Timer();
    私人上下文CTX;

    公众的IBinder onBind(意向为arg0)
    {
          返回null;
    }

    公共无效的onCreate()
    {
          super.onCreate();
          CTX =这一点;
          startService();
    }

    私人无效startService()
    {
        timer.scheduleAtFixedRate(新MainTask的(),0,5000);
    }

    私有类MainTask的扩展的TimerTask
    {
        公共无效的run()
        {
            toastHandler.sendEmptyMessage(0);
        }
    }

    公共无效的onDestroy()
    {
          super.onDestroy();
          Toast.makeText(本,服务已停止......,Toast.LENGTH_SHORT).show();
    }

    私人最终处理程序toastHandler =新的处理程序()
    {
        @覆盖
        公共无效的handleMessage(信息MSG)
        {
            Toast.makeText(getApplicationContext(),测试,Toast.LENGTH_SHORT).show();
        }
    };
}
 

I am having problems running a timer in a service I have created. The task that the timer calls simply isn't called. I know that the service starts as I have put toasts within it and they are called, but not when they are within the timer. Help appreciated.

service class:

public class LocalService extends Service
{
    private static Timer timer = new Timer(); 
    private Context ctx;

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

    public void onCreate() 
    {
          super.onCreate();
          ctx = this; 
          startService();
    }

    private void startService()
    {           
        timer.scheduleAtFixedRate(new mainTask(), 0, 5000);
    }

    private class mainTask extends TimerTask
    { 
        public void run() 
        {
            Toast.makeText(ctx, "test", Toast.LENGTH_SHORT).show();
        }
    }    

    public void onDestroy() 
    {
          super.onDestroy();
          Toast.makeText(this, "Service Stopped ...", Toast.LENGTH_SHORT).show();
    }    
}

Main class:

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    startService(new Intent(RingerSchedule.this, LocalService.class));      
}   

解决方案

Android does not allow UI events like Toasts from outside the main thread. The run is getting called, but the Toast is being ignored.

To create the Toast on the UI thread, you can use a Handler and an empty Message like so:

public class LocalService extends Service
{
    private static Timer timer = new Timer(); 
    private Context ctx;

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

    public void onCreate() 
    {
          super.onCreate();
          ctx = this; 
          startService();
    }

    private void startService()
    {           
        timer.scheduleAtFixedRate(new mainTask(), 0, 5000);
    }

    private class mainTask extends TimerTask
    { 
        public void run() 
        {
            toastHandler.sendEmptyMessage(0);
        }
    }    

    public void onDestroy() 
    {
          super.onDestroy();
          Toast.makeText(this, "Service Stopped ...", Toast.LENGTH_SHORT).show();
    }

    private final Handler toastHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();
        }
    };    
}

这篇关于一个服务中的Andr​​oid计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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