title: countdown timer in listview
body: <p>I have implemented countdown timer in listview.But problem is that when i close my app and reopen it timer gets reset from beginning.I want timer to run even if app is closed.For eg-If time is 10min and i close app and open after 6 min timer must show remaining time as 4 min.</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends Activity {
  
      private ListView lvItems;
      private List&lt;Product&gt; lstProducts;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          lvItems = (ListView) findViewById(R.id.lvItems);
          lstProducts = new ArrayList&lt;&gt;();
          lstProducts.add(new Product("A", System.currentTimeMillis() + 10000));
          lstProducts.add(new Product("B", System.currentTimeMillis() + 250000));
          lstProducts.add(new Product("C", System.currentTimeMillis() + 200000));
          lstProducts.add(new Product("D", System.currentTimeMillis() + 300000));
  
  
          lvItems.setAdapter(new CountdownAdapter(MainActivity.this, lstProducts));
      }
  
  }
  </code></pre>
  
  <p><strong>Product.java</strong></p>
  
  <pre><code>public class Product {
      String name;
      long expirationTime;
  
      public Product(String name, long expirationTime) {
          this.name = name;
          this.expirationTime = expirationTime;
      }
  }
  </code></pre>
  
  <p><strong>CountdownAdapter.java</strong></p>
  
  <pre><code>public class CountdownAdapter extends ArrayAdapter&lt;Product&gt; {
  
      private LayoutInflater lf;
      private List&lt;ViewHolder&gt; lstHolders;
      private Handler mHandler = new Handler();
      private Runnable updateRemainingTimeRunnable = new Runnable() {
          @Override
          public void run() {
              synchronized (lstHolders) {
                  long currentTime = System.currentTimeMillis();
                  for (ViewHolder holder : lstHolders) {
                      holder.updateTimeRemaining(currentTime);
                  }
              }
          }
      };
  
      public CountdownAdapter(Context context, List&lt;Product&gt; objects) {
          super(context, 0, objects);
          lf = LayoutInflater.from(context);
          lstHolders = new ArrayList&lt;&gt;();
          startUpdateTimer();
      }
  
      private void startUpdateTimer() {
          Timer tmr = new Timer();
          tmr.schedule(new TimerTask() {
              @Override
              public void run() {
                  mHandler.post(updateRemainingTimeRunnable);
              }
          }, 1000, 1000);
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          ViewHolder holder = null;
          if (convertView == null) {
              holder = new ViewHolder();
              convertView = lf.inflate(R.layout.list_item, parent, false);
              holder.tvProduct = (TextView) convertView.findViewById(R.id.tvProduct);
              holder.tvTimeRemaining = (TextView) convertView.findViewById(R.id.tvTimeRemaining);
              convertView.setTag(holder);
              synchronized (lstHolders) {
                  lstHolders.add(holder);
              }
          } else {
              holder = (ViewHolder) convertView.getTag();
          }
  
          holder.setData(getItem(position));
  
          return convertView;
      }
  
  
      class ViewHolder {
          TextView tvProduct;
          TextView tvTimeRemaining;
          Product mProduct;
  
          public void setData(Product item) {
              mProduct = item;
              tvProduct.setText(item.name);
              updateTimeRemaining(System.currentTimeMillis());
          }
  
          public void updateTimeRemaining(long currentTime) {
              long timeDiff = mProduct.expirationTime - currentTime;
              if (timeDiff &gt; 0) {
                  int seconds = (int) (timeDiff / 1000) % 60;
                  int minutes = (int) ((timeDiff / (1000 * 60)) % 60);
                  int hours = (int) ((timeDiff / (1000 * 60 * 60)) % 24);
                  tvTimeRemaining.setText(hours + " hrs " + minutes + " mins " + seconds + " sec");
              } else {
                  tvTimeRemaining.setText("Expired!!");
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>activity_main.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/activity_main"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context="com.example.ram.asynctimer.MainActivity"&gt;
  
      &lt;ListView
          android:id="@+id/lvItems"
          android:layout_width="match_parent"
          android:layout_height="match_parent" /&gt;
  
  
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>list_item.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical" android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;TextView
          android:id="@+id/tvProduct"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_margin="10dp"
          android:text="Product Name"
          android:textSize="16dp"
          android:textStyle="bold" /&gt;
  
      &lt;TextView
          android:id="@+id/tvTimeRemaining"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_margin="10dp"
          android:text="Time Remaining : " /&gt;
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>MyService.java</p>
  
  <pre><code>public class MyService extends Service {
  
      @Override
      public IBinder onBind(Intent intent) {
          // TODO: Return the communication channel to the service.
          //throw new UnsupportedOperationException("Not yet implemented");
          return null;
      }
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          // do your jobs here
          return super.onStartCommand(intent, flags, startId);
      }
  }
  </code></pre>