android:实现定时任务

发布于 2024-12-23 21:59:51 字数 2606 浏览 1 评论 0原文

我正在制作一个应用程序,如果应用程序打开,我必须在 1 小时后发送 GPS 协调,并且一次在注销后打开应用程序意味着当应用程序启动时,GPS 协调将发送到服务器,并且如果应用程序保持打开状态1小时内,将发送gps坐标,如果应用程序在1小时前关闭,则应用程序将不会发送gps坐标。我的代码如下:

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();
        }

I am making an app in which i have to send gps coordinated after 1 hour if the app is open and one time when app is opened after logging out means when the app starts the gps coordinated will be send to server and if the app remained opened for 1 hour , gps coordinates will be send and if the app gets closed before 1 hour , the app will not send gps coordinates.My code is as follows:

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();
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小伙你站住 2024-12-30 21:59:51

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

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

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

mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    mlocListener = new MyLocationListener();
    mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,3600000, 40,  mlocListener);

其中 3600000 是时隙,40is 距离(以米为单位),mloc 管理器和 mloc 侦听器是位置管理器和位置侦听器

Just change the given code accordingly:

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

and set the time and mininum distance in location manager which is as follows:

mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    mlocListener = new MyLocationListener();
    mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,3600000, 40,  mlocListener);

where 3600000 is the time slot and 40is distance in metres and mloc manager and mloc listener are location manager and location listener

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文