首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >android:实现定时器任务

android:实现定时器任务
EN

Stack Overflow用户
提问于 2011-12-30 14:49:21
回答 1查看 1K关注 0票数 0

我正在制作一个应用程序,其中我必须发送全球定位系统协调后1小时,如果应用程序是打开的,一次当应用程序被打开后注销意味着当应用程序启动时,全球定位系统协调将被发送到服务器,如果应用程序保持打开1小时,gps坐标将被发送,如果应用程序在1小时前关闭,应用程序将不会发送全球定位系统coordinates.My代码如下:

代码语言:javascript
复制
TimerTask timer_task = new TimerTask() {

            public void run()
            {
                        Log.v(".............................................", "Timer Task Started");
                        System.out.println("@@@@@@@@@@ timer task started  IN TRACKER11111");
                        try
                        {
                            // if (locn != null)
                            {

                                Calendar cal = Calendar.getInstance();
                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
                                String time = sdf.format(cal.getTime());
                                String xml = xml parameters being send to server
                                System.out.println(xml);
                                System.out.println("hello");

                                System.out.println("Xml is : "+xml);

                            //  FileSave obj9=new FileSave();
                            //  obj9.Save(xml);


                                int len = xml.length();

                                byte[] data = xml.getBytes();
                                System.out.println("Length =****************************  " + len);

                                System.out.println("Stream Closed");

                                conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                                if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() && conMgr.getActiveNetworkInfo().isConnected())
                                {
                                    //if (gps.equals("yes"))
                                    {
                                        new Connection(data);
                                    }
                                }
                                else
                                {
                                }

                            }
                            }
                        catch (Exception e)
                        {
                            e.printStackTrace();
                        }

            }
        };
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(timer_task, 4000, 3600000);   






         }
        catch (Exception e1) {
            e1.printStackTrace();
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-04 18:51:46

只需相应地更改给定的代码:

代码语言:javascript
复制
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(timer_task, 0, 3600000);   

在位置管理器中设置时间和最小距离,如下所示:

代码语言:javascript
复制
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    mlocListener = new MyLocationListener();
    mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,3600000, 40,  mlocListener);

其中3600000是时隙,40是以米为单位的距离,mloc管理器和mloc监听器是位置管理器和位置监听器

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8676945

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档