package app.test;
import android.app.Activity;
import android.app.IntentService;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
public class Test extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
logEvent("CREATE");
}
@Override
public void onStart() {
super.onStart();
logEvent("START");
}
@Override
public void onResume() {
super.onResume();
logEvent("RESUME");
}
@Override
public void onPause() {
super.onPause();
logWarning("PAUSE");
}
@Override
public void onStop() {
super.onStop();
logWarning("STOP");
}
@Override
public void onDestroy() {
super.onDestroy();
logWarning("DESTROY");
}
private void logEvent(String event) {
Intent intent = new Intent(this, OperationsManager.class);
intent.setAction(OperationsManager.ACTION_EVENT);
intent.putExtra(OperationsManager.EXTRA_NAME, event);
startService(intent);
}
private void logWarning(String event) {
Intent intent = new Intent(this, OperationsManager.class);
intent.setAction(OperationsManager.ACTION_WARNING);
intent.putExtra(OperationsManager.EXTRA_NAME, event);
startService(intent);
}
}
class OperationsManager extends IntentService {
public static final String ACTION_EVENT = "ACTION_EVENT";
public static final String ACTION_WARNING = "ACTION_WARNING";
public static final String ACTION_ERROR = "ACTION_ERROR";
public static final String EXTRA_NAME = "eventName";
private static final String LOGTAG = "EventLogger";
private IntentFilter matcher;
public OperationsManager() {
super("OperationsManager");
matcher = new IntentFilter();
matcher.addAction(ACTION_EVENT);
matcher.addAction(ACTION_WARNING);
matcher.addAction(ACTION_ERROR);
}
@Override
protected void onHandleIntent(Intent intent) {
if (!matcher.matchAction(intent.getAction())) {
Toast.makeText(this, "OperationsManager: Invalid Request",
Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.equals(intent.getAction(), ACTION_EVENT)) {
logEvent(intent.getStringExtra(EXTRA_NAME));
}
}
private void logEvent(String name) {
try {
Thread.sleep(5000);
Log.i(LOGTAG, name);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}