Back to project page ServicesTutorial.
The source code is released under:
Apache License
If you think the Android project ServicesTutorial listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.yanlu.android.services.app.utils; /*from w w w.java 2s . c o m*/ import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; /** * User: captain_miao * Date: 14-5-16 * Time: ????9:18 */ public class DeferWorkHandler extends Handler { public static final String tag = "DeferWorkHandler"; //Keep track of how many times we sent the message private int count = 0; //A parent driver activity we can use //to inform of status. private Activity parentActivity = null; //During construction we take in the parent //driver activity. public DeferWorkHandler(Activity inParentActivity) { parentActivity = inParentActivity; } @Override public void handleMessage(Message msg) { String pm = new String( "message called:" + count + ":" + msg.getData().getString("message")); Log.d(tag, pm); // this.printMessage(pm); if (count > 5) { return; } count++; sendTestMessage(1); } public void sendTestMessage(long interval) { Message m = this.obtainMessage(); prepareMessage(m); this.sendMessageDelayed(m, interval * 1000); } public void doDeferredWork() { count = 0; sendTestMessage(1); } public void prepareMessage(Message m) { Bundle b = new Bundle(); b.putString("message", "Hello World"); m.setData(b); return; } //This method just prints a message //in a text box in the parent activity. private void printMessage(String xyz) { // parentActivity.appendText(xyz); } } }