From: Eugene Crosser Date: Sat, 25 Feb 2012 12:35:45 +0000 (+0400) Subject: Stay alive unless closed from menu X-Git-Url: http://average.org/gitweb/?a=commitdiff_plain;h=HEAD;p=WhereAmI.git Stay alive unless closed from menu --- diff --git a/src/org/average/whereami/WhereAmI.java b/src/org/average/whereami/WhereAmI.java index be72fea..94aba8f 100644 --- a/src/org/average/whereami/WhereAmI.java +++ b/src/org/average/whereami/WhereAmI.java @@ -27,6 +27,7 @@ import android.preference.PreferenceManager; import android.text.format.Time; import android.util.Log; import android.view.View; +import android.view.KeyEvent; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; @@ -45,6 +46,8 @@ public class WhereAmI extends Activity private SharedPreferences prefs; private PersistentStore store; private Random random = new Random(); + private Boolean revive = false; + private Context context; private class UpdateTarget { private TextView tv; @@ -115,6 +118,20 @@ public class WhereAmI extends Activity private Handler mHandler = new Handler(); + private Runnable reviveSelf = new Runnable() { + public void run() { + Log.v(TAG, "trying to revive self"); + Intent intent = new Intent(Intent.ACTION_MAIN); + intent.addCategory(Intent.CATEGORY_LAUNCHER ); + intent.setClass(context, WhereAmI.class); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); + intent.addFlags(Intent.FLAG_FROM_BACKGROUND); + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + startActivity(intent); + } + }; + private Runnable updateClock = new Runnable () { public void run() { long now = System.currentTimeMillis(); @@ -215,6 +232,7 @@ public class WhereAmI extends Activity @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + context = this; if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO) { Log.v(TAG, "Disabling keepalive for build version " + Build.VERSION.SDK_INT); @@ -261,6 +279,7 @@ public class WhereAmI extends Activity } else { getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); } + revive = true; mHandler.post(updateClock); mHandler.post(updateCal); mHandler.post(updateInfo); @@ -276,6 +295,10 @@ public class WhereAmI extends Activity mHandler.removeCallbacks(updateClock); mHandler.removeCallbacks(updateCal); mHandler.removeCallbacks(updateInfo); + if (revive) { + Log.v(TAG, "scheduling revival"); + mHandler.postDelayed(reviveSelf, 6000); // 6 second delay + } } /** Called when the activity is destroyed. */ @@ -298,6 +321,7 @@ public class WhereAmI extends Activity public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.quit: + revive = false; finish(); return true; case R.id.authorize: @@ -312,4 +336,13 @@ public class WhereAmI extends Activity return super.onOptionsItemSelected(item); } } + + /** Override "back" button. Can still quit via menu. */ + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + if (keyCode == KeyEvent.KEYCODE_BACK) { + return true; + } + return super.onKeyDown(keyCode, event); + } }