我怎样重复的方法,每10分钟后,一个按钮preSS和其他按钮preSS系铃人 [英] How do I repeat a method every 10 minutes after a button press and end it on another button press

查看:112
本文介绍了我怎样重复的方法,每10分钟后,一个按钮preSS和其他按钮preSS系铃人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个Android应用程序检索手机的当前位置,并把它发送过一个网络服务器。 我希望能够以preSS启动按钮,并有应用程序继续检索并以predetermined间隔(比如每隔10分钟)发送的位置,然后把它停在另一个按钮preSS。

I am writing an Android app that retrieves the phone's current location and sends it too a webserver. I want to be able to press a start button and have the app continue to retrieve and send the location at a predetermined interval (say every 10 minutes) and then have it stop on another button press.

下面是$ C $下我的按钮:

Here is the code for my buttons:

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

   startButton.setOnClickListener(new OnClickListener() {
    @Override
    //When the button is clicked
    public void onClick(View v) {
            finishButton.setEnabled(true);
            startButton.setEnabled(false);

            //Loops every 10mins
            pingCurrentLocation();

        }
    });

  finishButton.setOnClickListener(new OnClickListener() {
    @Override
    //When the button is clicked
    public void onClick(View v) {
            startButton.setEnabled(true);
            finishButton.setEnabled(false);

            pingCurrentLocation();

        }
    });  
}

pingCurrentLocation是,获取的位置,并发送它。功能

pingCurrentLocation is the function that gets the location and sends it.

我知道,使用AlarmManager可能会达到什么样的,但我想我已经无法作出任何它的意义。是否有任何明确的步骤或模板,将工作在我的情况。

I know that using an AlarmManager would probably achieve what I want but I have been unable to make sense of any of it. Are there any clear steps or templates that will work in my situation.

推荐答案

创建一个的BroadcastReceiver

public class AlarmReceiver extends BroadcastReceiver
{   

 @Override
 public void onReceive(Context context, Intent intent)
  {   
    //get and send location information
  }
}

和添加相同的到你的 AndroidManifest ,以便接收器注册

and add the same to your AndroidManifest so that the Receiver is registered

<receiver
    android:name="com.coderplus.AlarmReceiver"
    android:exported="false">
</receiver>

现在你可以设置从活动重复报警,将调用接收器每10分钟:

Now you can set a repeating alarm from your Activity which will invoke the receiver every 10 minutes:

AlarmManager alarmManager=(AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),600000,
                                                                      pendingIntent);

和取消报警,呼叫取消() AlarmManager 使用等效 PendingIntent

and to cancel the alarm, call cancel() on the AlarmManager using an equivalent PendingIntent

AlarmManager alarmManager=(AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmManager.cancel(pendingIntent);


如果你不想使用 AlarmManager / 的BroadcastReceiver ,事遂所愿,这将帮助你。在你去了,检查 - <一个href="http://stackoverflow.com/questions/14579034/difference-between-timer-and-alarmmanager">difference计时器之间alarmmanager


or if you don't want to use AlarmManager / BroadcastReceiver, then something like this will help you out. Before you go for it, check - difference between timer and alarmmanager

private class MyTimerTask extends TimerTask {
    @Override
    public void run() {           
      //get and send location information 
    }
}

初​​始化定时器定时器任务:

Timer myTimer = new Timer();
MyTimerTask myTimerTask= new MyTimerTask();

停止或启动定时器

//to Stop
myTimer.cancel();
//to start
myTimer.scheduleAtFixedRate(myTimerTask, 0, 600000); //(timertask,delay,period)

请参阅 <一href="http://developer.android.com/reference/java/util/TimerTask.html">http://developer.android.com/reference/java/util/TimerTask.html

<一个href="http://developer.android.com/reference/java/util/Timer.html">http://developer.android.com/reference/java/util/Timer.html

这篇关于我怎样重复的方法,每10分钟后,一个按钮preSS和其他按钮preSS系铃人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆