Android上的后台进程计时器 [英] Background process timer on android

查看:221
本文介绍了Android上的后台进程计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让进程计时器运行并使其在android上在后台运行(从单击按钮开始).

I'm trying to get a process timer to run and keep it running in the background on android (starts with a button click).

计时器必须打开30秒,并且甚至应该在后台继续增加应用程序(使用主页按钮并关闭电源/关闭屏幕).

The timer must be on 30 seconds and should even continue growing application in the background (with home button and power / screen off).

我该怎么做?我尝试使用服务和处理程序,但无法正常工作...

How can I do this? I tried with service and handler but not working ...

编辑

我的服务跟踪(过程需要30秒)

My service tracking (process with 30 sec)

public class TrackingService extends IntentService {

    private Handler mHandler;
    private Runnable mRunnable;

    public TrackingService() {

        super("TrackingService");

    }

    public TrackingService(String name) {

        super(name);

    }

    @Override
    protected void onHandleIntent(Intent intent) {

        long timer = 30000;

        mHandler = new Handler();
        mRunnable = new Runnable() {

            @Override
            public void run() {

                    //TODO - process with update timer for new 30 sec

                    mHandler.postDelayed(this, timer);

            }
        };

        mHandler.postDelayed(mRunnable, timer);

    }

}

我的点击按钮:

mButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        //TODO - start first time and it continued every 30 seconds and continue in the background
        startService(Intent intent = new Intent(this, TrackingService.class));

    }
});

推荐答案

好吧,首先,我真的不知道我是否正确回答了您的问题.但是我想如果我没记错的话,您想要一个每30秒执行一次的计时器.如果是这样,请执行以下操作:

Ok, first of all, I really don't know if I got your question quite right. But I think you want a timer that's being executed every 30 seconds ,if i'm not mistaken. If so, do as following:

AlarmManager

注意:该类提供对系统警报服务的访问.这些使您可以计划您的应用程序在将来的某个时间运行.当警报响起时,系统会广播已为其注册的Intent,如果尚未已在运行该目标应用程序,则会自动启动该目标应用程序.设备睡眠时会保留已注册的警报(如果警报在这段时间内关闭,可以选择将其唤醒),但是如果关闭并重新启动,则会清除该警报.strong>

Note: This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.

示例:

在您的 onClick()中注册您的计时器:

in your onClick() register your timer:

int repeatTime = 30;  //Repeat alarm time in seconds
AlarmManager processTimer = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, processTimerReceiver.class);   
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,  intent, PendingIntent.FLAG_UPDATE_CURRENT);
//Repeat alarm every second
processTimer.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),repeatTime*1000, pendingIntent); 

和您的 processTimerReceiver类:

//This is called every second (depends on repeatTime)
public class processTimerReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        //Do something every 30 seconds
    }
}

请不要忘记在您的 Manifest.XML

<receiver android:name="processTimer" >
   <intent-filter>
       <action android:name="processTimerReceiver" >
       </action>
   </intent-filter>
</receiver>

如果您想取消该警报,请执行以下操作:使用它来这样做:

If you ever want to cancel the alarm: use this to do so:

//Cancel the alarm
Intent intent = new Intent(this, processTimerReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);

希望这对您有所帮助.

PS::如果这不完全是您想要的,请在评论中保留它,或者如果有人要编辑它,请这样做.

PS: if this is not exactly what u want, please leave it in the comments, or if someone wants to edit this, please do so.

这篇关于Android上的后台进程计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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