2 llx2007 llx2007 于 2015.06.04 09:41 提问

android进度条在gridview中

我的gridview种有个进度条和开始及结束两个按钮,我点击开始,我希望他可以一直按照我的赋值更新进度,当我按下结束按钮时他结束掉。这个怎么实现呢?求大师指点迷津!

5个回答

LogicTeamLeader
LogicTeamLeader   2015.06.04 09:48

创建一个服务,这个服务可以读取你的赋值并广播进度,你的界面可以收听这个服务的广播并自己更新进度条。这是通用的做法。

llx2007
llx2007 我创建了,可是不行呀。我贴上代码帮我看看吧,大师
2 年多之前 回复
Arnold9009
Arnold9009   2015.06.04 10:02

thread + handler
thread负责赋值的变化,然后发消息
handler负责更新进度条
通过btn的点击响应来控制thread的运行

llx2007
llx2007 附上代码,帮我看看么,我做的一直在死机
2 年多之前 回复
llx2007
llx2007   2015.06.04 10:10

myadapter.java(自定义适配器)
tock_on.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                    if (tock_flag) {
                    } else {
                        tock_flag = true;
                        tock_on.setBackgroundResource(0);
                        tock_on.setBackgroundResource(R.drawable.tock_on_btn);
                        tock_off.setBackgroundResource(0);
                        tock_off.setBackgroundResource(R.drawable.tock_off_btn);
                        search_voice.setBackgroundResource(0);
                        search_voice.setBackgroundResource(R.drawable.vocie_press);


                        //计时器
                        if (null != timer) {
                            task.cancel();
                            task = null;
                            timer.cancel(); // Cancel timer
                            timer.purge();
                            timer = null;
                            handler.removeMessages(msg.what);
                            mlCount = 0;
                            if (SETTING_SECOND_ID == settingTimerUnitFlg) {
                                // second
                                tvTime.setText(R.string.init_time_second);
                            } else if (SETTING_100MILLISECOND_ID == settingTimerUnitFlg) {
                                // 100 millisecond
                                tvTime.setText(R.string.init_time_100millisecond);
                            }
                        }
                        task = new TimerTask() {
                            public void run() {
                                if (null == msg) {
                                    msg = new Message();
                                } else {
                                    msg = Message.obtain();
                                }
                                msg.what = 1;
                                handler.sendMessage(msg);
                            }

                        };
                        timer = new Timer(true);
                        timer.schedule(task, mlTimerUnit, mlTimerUnit); // set timer duration


                    }
                }

            });
                         tock_off.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                    if(tock_flag){//只对说话按钮负责
                        tock_off.setBackgroundResource(0);
                        tock_off.setBackgroundResource(R.drawable.tock_off);
                        tock_on.setBackgroundResource(0);
                        tock_on.setBackgroundResource(R.drawable.tock_on);
                        search_voice.setBackgroundResource(0);
                        search_voice.setBackgroundResource(R.drawable.vocie);

                        tock_flag = false;
                        voice_flag = true;

                        try {
                            task.cancel();
                            timer.cancel(); // Cancel timer
                            timer.purge();
                            handler.removeMessages(msg.what);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                }
            });

final Handler mHandler =new Handler(){
                public void handleMessage(Message msg) {
                    if(msg.what==0x111){
                        volum.setProgress(volum_value); //更新进度
                    }else{
                        volum.setVisibility(View.GONE); //设置进度条不显示,并且不占用空间
                    }
                }
            };
  new Thread(new Runnable() {
                    public void run() {
                        while (true) {
                            volum_value = doWork(); //获取耗时操作完成的百分比
                            Message m=new Message();
                            if(tock_flag){
                                m.what=0x111;
                                mHandler.sendMessage(m);    //发送信息
                            }else{
                                m.what=0x110;
                                mHandler.sendMessage(m);    //发送消息
                                break;
                            }
                        }
                    }
                    //模拟一个耗时操作
                    private byte doWork() {
                        volum_value+=Math.random()*10;  //改变完成进度
                        try {
                            Thread.sleep(200);      //线程休眠200毫秒
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        return volum_value; //返回新的进度
                    }
                }).start(); //开启一个线程

llx2007
llx2007   2015.06.04 10:11

myadapter.java(自定义适配器)
tock_on.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                    if (tock_flag) {
                    } else {
                        tock_flag = true;
                        tock_on.setBackgroundResource(0);
                        tock_on.setBackgroundResource(R.drawable.tock_on_btn);
                        tock_off.setBackgroundResource(0);
                        tock_off.setBackgroundResource(R.drawable.tock_off_btn);
                        search_voice.setBackgroundResource(0);
                        search_voice.setBackgroundResource(R.drawable.vocie_press);


                        //计时器
                        if (null != timer) {
                            task.cancel();
                            task = null;
                            timer.cancel(); // Cancel timer
                            timer.purge();
                            timer = null;
                            handler.removeMessages(msg.what);
                            mlCount = 0;
                            if (SETTING_SECOND_ID == settingTimerUnitFlg) {
                                // second
                                tvTime.setText(R.string.init_time_second);
                            } else if (SETTING_100MILLISECOND_ID == settingTimerUnitFlg) {
                                // 100 millisecond
                                tvTime.setText(R.string.init_time_100millisecond);
                            }
                        }
                        task = new TimerTask() {
                            public void run() {
                                if (null == msg) {
                                    msg = new Message();
                                } else {
                                    msg = Message.obtain();
                                }
                                msg.what = 1;
                                handler.sendMessage(msg);
                            }

                        };
                        timer = new Timer(true);
                        timer.schedule(task, mlTimerUnit, mlTimerUnit); // set timer duration


                    }
                }

            });
                         tock_off.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                    if(tock_flag){//只对说话按钮负责
                        tock_off.setBackgroundResource(0);
                        tock_off.setBackgroundResource(R.drawable.tock_off);
                        tock_on.setBackgroundResource(0);
                        tock_on.setBackgroundResource(R.drawable.tock_on);
                        search_voice.setBackgroundResource(0);
                        search_voice.setBackgroundResource(R.drawable.vocie);

                        tock_flag = false;
                        voice_flag = true;

                        try {
                            task.cancel();
                            timer.cancel(); // Cancel timer
                            timer.purge();
                            handler.removeMessages(msg.what);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                }
            });

final Handler mHandler =new Handler(){
                public void handleMessage(Message msg) {
                    if(msg.what==0x111){
                        volum.setProgress(volum_value); //更新进度
                    }else{
                        volum.setVisibility(View.GONE); //设置进度条不显示,并且不占用空间
                    }
                }
            };
  new Thread(new Runnable() {
                    public void run() {
                        while (true) {
                            volum_value = doWork(); //获取耗时操作完成的百分比
                            Message m=new Message();
                            if(tock_flag){
                                m.what=0x111;
                                mHandler.sendMessage(m);    //发送信息
                            }else{
                                m.what=0x110;
                                mHandler.sendMessage(m);    //发送消息
                                break;
                            }
                        }
                    }
                    //模拟一个耗时操作
                    private byte doWork() {
                        volum_value+=Math.random()*10;  //改变完成进度
                        try {
                            Thread.sleep(200);      //线程休眠200毫秒
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        return volum_value; //返回新的进度
                    }
                }).start(); //开启一个线程

llx2007
llx2007 放在error上面什么都没有
2 年多之前 回复
llx2007
llx2007 回复Arnold9009: 没有日志,定时器刚开始3秒是合适的,后面直接不对了,以下走几秒,还卡,最后界面死了,什么都动不了了,最后出来个未响应,结束。
2 年多之前 回复
Arnold9009
Arnold9009 死机日志贴上了看看
2 年多之前 回复
llx2007
llx2007   2015.06.04 10:31

06-04 10:34:30.390 424-591/? E/Watchdog﹕ !@Sync 12144
06-04 10:34:34.164 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:34:34.164 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:34:39.159 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:34:39.169 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:34:44.194 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:34:44.194 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:34:44.744 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:34:44.744 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:34:49.779 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:34:49.779 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:34:54.794 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:34:54.794 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:34:59.809 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:34:59.809 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:35:00.389 424-591/? E/Watchdog﹕ !@Sync 12145
06-04 10:35:04.824 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:35:04.824 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:35:09.849 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:35:09.849 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:35:14.834 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:35:14.834 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:35:15.394 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:35:15.394 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:35:20.429 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:35:20.429 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:35:25.434 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:35:25.434 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:35:30.389 424-591/? E/Watchdog﹕ !@Sync 12146
06-04 10:35:30.439 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:35:30.439 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:35:35.444 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:35:35.444 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:35:40.459 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:35:40.459 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:35:45.463 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:35:45.463 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:35:50.478 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:35:50.478 10502-10502/? E/MtpService﹕ battPlugged Type : 2
06-04 10:35:55.493 10502-10502/? E/MtpService﹕ In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
06-04 10:35:55.493 10502-10502/? E/MtpService﹕ battPlugged Type : 2
这些算么

Csdn user default icon
上传中...
上传图片
插入图片