定时器属于服务或广播接收器吗?

时间:2017-01-10 13:04:18

标签: android

我们都知道计时器应用程序的作用,我们也知道它在后台运行。所以我的问题是,它属于哪个?它属于服务或广播接收器吗?

1 个答案:

答案 0 :(得分:0)

做你想做的事非常简单

public class MyService extends Service {

    Timer timer;
    Handler helper;

    public static long TIME = 60000; // Repeats every 1 minute


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

        timer = new Timer();
        helper = new Handler(Looper.getMainLooper());

        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {

                // Actions to be made every 1 minute

            }
        }, 0, TIME);

    }


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

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