简体   繁体   English

在Android上的Java中每60秒重复一种方法

[英]Repeating a method every 60 seconds in Java on Android

I am busy with a application for my phone for a project. 我正忙于为我的手机项目申请。 I am not a programmer, so have learnt a bit of java for android so far. 我不是程序员,因此到目前为止,我已经为Android学习了一些Java。

I am stuck on running a method every 60 Seconds while the application is running on the phone. 在应用程序在手机上运行时,我每60秒运行一次方法。

The application uses the GPS and then sends a User Id & Gps co-ords to a server. 该应用程序使用GPS,然后将用户ID和Gps坐标发送到服务器。

So I have a Method (getLoc) that gets the Location & then calls the send to server method and save to SD card method. 所以我有一个方法(getLoc),它获取位置信息,然后调用发送到服务器方法并保存到SD卡方法。

This is for proof of concept & I only need to run the application over the next few days in my car while the phone is connected to the car charger & not allowing it to sleep. 这是出于概念验证的目的,我只需要在手机连接到车载充电器且不让其进入睡眠状态的情况下,在接下来的几天内在汽车中运行该应用程序。 I need to log some "test" data (GPS Coords) while I drive around over the next few days. 在接下来的几天里,我需要记录一些“测试”数据(GPS坐标)。

I am just looking for the easiest way to repeat the method every 60 seconds that sends the data to the server while the Location manager runs & gets the location constantly.. 我只是在寻找最简单的方法,即每60秒重复一次将位置数据发送到服务器,同时位置管理器不断运行并获取位置的方法。

How would I keep the method getLoc to run every 60 Seconds? 我如何保持getLoc方法每60秒运行一次?

A few problems with this approach: 这种方法的几个问题:

  1. You can not gather location data all the time, because when the device goes to sleep mode user processes stop executing and network is suspended. 您无法一直收集位置数据,因为当设备进入睡眠模式时,用户进程将停止执行,并且网络将被挂起。

  2. Running GPS all the time will drain battery in a matter of hours. 始终运行GPS将在数小时内耗尽电池。

  3. If you force to use network every 60sec (via AlarmManager, waking the phone), then network will never shutdown and this will kill battery even faster then GPS. 如果您每60秒强制使用一次网络(通过AlarmManager,唤醒电话),则网络将永远不会关闭,这将比GPS更快地消耗电池电量。

  4. Do you want to update the data even if user does not move? 即使用户不动也要更新数据吗? Because, on average, users do not move often. 因为平均而言,用户不经常移动。

If you want to handle location data the right way, then it's more complex than simply gathering location at 60 sec interval. 如果您想以正确的方式处理位置数据,则比仅以60秒的间隔收集位置更为复杂。 I highly suggest you read this excellent blog post: http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html 我强烈建议您阅读以下出色的博客文章: http : //android-developers.blogspot.com/2011/06/deep-dive-into-location.html

And the part 2: http://blog.radioactiveyak.com/2011/06/deep-dive-into-location-part-2-being.html 而第2部分: http : //blog.radioactiveyak.com/2011/06/deep-dive-into-location-part-2-being.html

请参阅问题,以及有关Android计时器的参考文档。

Check out the Handler class for delayed method execution, but if you're executing a method every 60 seconds say goodbye to your battery. 签出Handler类以延迟执行方法,但是如果每60秒执行一次方法,请告别电池。

http://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable , long) http://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable,long

The "easy" answer for repeating a task over the time: 在一段时间内重复执行任务的“简单”答案:

final static long REFRESH=60*1000;
final static int SUBJECT=0;
Handler locationHandler= new Handler(){
    public void handleMessage(Message msg){
        if (msg.what==SUBJECT){
            getLoc();
            this.sendEmptyMessageDelayed(SUBJECT, REFRESH);
        }
    }
};

For stating the track: 为了说明轨道:

       locationHandler.sendEmptyMessage(SUBJECT);

for stopping it: 停止它:

       locationHandler.removeMessages(SUBJECT);

But this solution is not optimal and will stop working if the phone goes to sleep mode. 但是此解决方案不是最佳解决方案,如果电话进入睡眠模式,它将停止工作。 For doing it correctly you should make a LocationListener or an AlarmManager . 为了正确执行此操作,您应该制作一个LocationListenerAlarmManager

As the other posts say 60 seconds as refresh will vaporize the battery 正如其他帖子所说的那样,刷新60秒会蒸发掉电池

//Method for the visibility of Resend button after 60 seconds passed

private void VisibleBtnResend60() {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {

                //call anything you want to perform

                mResendButton.setVisibility(View.VISIBLE);
                enableViews(mResendButton);
            }
        }, 60000);
    }

In Java you would create a Thread running in the background. 在Java中,您将创建一个在后台运行的线程。

I'm not sure that is the right way to do it on an Android. 我不确定这是在Android上执行此操作的正确方法。

Do you want this to only run while the user is looking at the application or also while it's still running in the background? 您是否希望仅在用户查看应用程序时还是在后台运行时运行此程序?

EDIT 编辑

Actually, instead of Thread use java.util.Timer and java.util.TimerTask. 实际上,可以使用java.util.Timer和java.util.TimerTask代替Thread。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM