当应用程序从后台删除时倒数计时器不工作

时间:2017-05-11 07:23:06

标签: android listview timer

在我的listview中,我有srno,名字,按钮和countdowntimer.Button可以在倒数计时器结束时点击。所有工作正常。但问题是,当关闭我的应用程序并再次打开它倒计时器从头开始重新启动。即使我关闭我的应用程序,我也希望倒数计时器能够运行。我从过去3天开始坚持到底。任何人都可以给我确切的代码plz.Following是我的代码 -

Second.java

public class SecondCustom extends BaseAdapter {
    Activity a;
    ArrayList<Model> flowerlist;
    private List<Viewholder> lstHolders;
    long timeDiff;



    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 SecondCustom(Activity a, ArrayList<Model> flowerlist) {
        this.a = a;
        this.flowerlist = flowerlist;
        lstHolders = new ArrayList<>();
        startUpdateTimer();
    }


    private void startUpdateTimer() {
        Timer tmr = new Timer();
        tmr.schedule(new TimerTask() {
            @Override
            public void run() {
                mHandler.post(updateRemainingTimeRunnable);
            }
        }, 1000, 1000);
    }


    @Override
    public int getCount() {
        return flowerlist.size();
    }

    @Override
    public Object getItem(int i) {
        return flowerlist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


public class Viewholder {
        TextView srno, name, ltime;
        Button buy;
        Model mModel;


        public void setData(Model item) {
            mModel = item;
            name.setText(item.name);
            updateTimeRemaining(System.currentTimeMillis());
        }

        public void updateTimeRemaining(long currentTime) {
            timeDiff = mModel.expirationTime - currentTime; // Changes -- remove long
            if (timeDiff > 0) {
                int seconds = (int) (timeDiff / 1000) % 60;
                int minutes = (int) ((timeDiff / (1000 * 60)) % 60);
                int hours = (int) ((timeDiff / (1000 * 60 * 60)) % 24);
                ltime.setText(hours + ":" + minutes + ":" + seconds);

                buy.setClickable(false);

            } else {
                buy.setClickable(true);
                ltime.setText("Buy Now!!");
                buy.setBackgroundColor(Color.parseColor("#e72929"));
            }
        }
    }

    @Override
    public View getView(final int i, View convertView, ViewGroup viewGroup) {
        Viewholder viewholder = null;
        if (convertView == null) {
            viewholder = new Viewholder();
            LayoutInflater li = a.getLayoutInflater();
            convertView = li.inflate(R.layout.secondcustom, viewGroup, false);
            viewholder.srno = (TextView) convertView.findViewById(R.id.sr);
            viewholder.name = (TextView) convertView.findViewById(R.id.flow);
            viewholder.buy = (Button) convertView.findViewById(R.id.st);
            viewholder.ltime = (TextView) convertView.findViewById(R.id.lefttime);
            convertView.setTag(viewholder);
            synchronized (lstHolders) {
                lstHolders.add(viewholder);
            }
        } else {
            viewholder = (Viewholder) convertView.getTag();
        }

        viewholder.setData((Model) getItem(i));


        final Model model = flowerlist.get(i);
        viewholder.srno.setText(valueOf(flowerlist.get(i).getSr()));
        viewholder.name.setText(valueOf(flowerlist.get(i).getName()));
        viewholder.ltime.setText(valueOf(flowerlist.get(i).getExpirationTime()));



        viewholder.buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(a, "Purchased" + " " + flowerlist.get(i).getName(), Toast.LENGTH_LONG).show();
            }
        });


        return convertView;
    }
}

SecondCustom.java

as select..

2 个答案:

答案 0 :(得分:0)

  

但问题是,当我关闭我的应用程序并再次打开时,倒数计时器从头开始重新启动。

这是一个正常的活动工作流程,再次启动时会重新创建(关闭后)。

  

即使关闭我的应用程序,我也希望倒数计时器能够运行。

那么你需要一项服务或AlarmManager。但请记住,当您的应用程序处于后台时,您无法触摸其UI。

对您来说,最好的方法可能是将活动状态保存到后台并在再次显示时恢复,这是一种在用户在应用程序之间切换时保存和恢复UI状态的方法:{{3 }}

答案 1 :(得分:0)

节省您退出应用程序的时间 - 当您再次启动应用程序时,查看已保存的时间,查看自那时起已经过了多长时间(使用System.currentTimeMillis())并相应地更新您的计时器。