Last active
March 20, 2018 11:30
-
-
Save li2/fccd6b79505ed8bd48439b9f9604c321 to your computer and use it in GitHub Desktop.
[Monitor Service status regularly, restart the service if it's died] #tags: android-service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by weiyi on 4/18/16. | |
*/ | |
public class MonitorService extends IntentService { | |
private static final String TAG = makeLogTag(MonitorService.class); | |
private static final int MONITOR_INTERVAL = 5 * 1000; | |
private Context mAppContext; | |
private Handler mHandler; | |
public MonitorService() { | |
super(TAG); | |
} | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
mAppContext = getApplicationContext(); | |
mHandler = new Handler(mAppContext.getMainLooper()); | |
mHandler.postDelayed(mMonitorRunnable, MONITOR_INTERVAL); | |
} | |
@Override | |
protected void onHandleIntent(Intent intent) { | |
} | |
private Runnable mMonitorRunnable = new Runnable() { | |
@Override | |
public void run() { | |
if (isDvrServiceDied()) { | |
startDvrService(); | |
LOGW(TAG, "DvrService is died, restart"); | |
} else { | |
LOGD(TAG, "DvrService is living"); | |
} | |
mHandler.postDelayed(mMonitorRunnable, MONITOR_INTERVAL); | |
} | |
}; | |
private boolean isDvrServiceDied() { | |
//return !DvrService.sIsLiving; | |
return false; | |
} | |
private void startDvrService() { | |
mAppContext.startService(new Intent(mAppContext, DvrService.class)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment