Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Runnable to be run every second only runs once in Fragment onCreateView()
- public class MyFragment extends Fragment {
- Calendar mCalendar;
- private Runnable mTicker;
- private Handler mHandler;
- TextView mClock;
- String mFormat;
- private boolean mClockStopped = false;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.meteo_widget, container, false);
- /*
- * Clock (from DigitalClock widget source)
- */
- mClock = (TextView) view.findViewById(R.id.clock);
- mCalendar = Calendar.getInstance();
- mHandler = new Handler();
- mTicker = new Runnable() {
- public void run() {
- if(mClockStopped) return;
- mCalendar.setTimeInMillis(System.currentTimeMillis());
- mClock.setText(DateFormat.format("hh:mm:ss", mCalendar));
- mClock.invalidate();
- long now = SystemClock.uptimeMillis();
- long next = now + (1000 - now % 1000);
- mHandler.postAtTime(mTicker, next);
- }
- };
- mTicker.run();
- return view;
- }
- @Override
- public void onResume()
- {
- super.onResume();
- mClockStopped = true;
- }
- @Override
- public void onPause()
- {
- mClockStopped = false;
- super.onPause();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement