Android每4秒运行一次任务 [英] Android run a task every 4 second

查看:621
本文介绍了Android每4秒运行一次任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要每4秒调用一次方法,即使设备处于睡眠状态,我也将警报管理器与服务Start_stick一起使用,服务名称为TransactionService.当设备处于活动状态并且每4秒钟调用一次该方法时,代码可以很好地工作,但是当屏幕锁定且设备处于睡眠状态时,调用将变得不准确.因此现在每2秒调用一次该方法,有时每1秒调用一次,5 ....

Hi i need to call a method every 4 seconds, even when the device is sleeping, i use alarm manager with service Start_stick, the service name is TransactionService. the code works well when the device is active and the method is called every exact 4 second, but when the screen is locked and device sleep the calling becomes inaccurate. so the method is now called every 2 seconds, sometimes every 1 sec,5 ....

这是我每4秒运行一次线程以调用方法的方式

this is how i run the thread to call method every 4 seconds

    AlarmManager mgr = (AlarmManager) getApplicationContext().getSystemService(
            Context.ALARM_SERVICE);
        Intent notificationIntent = new Intent(getApplicationContext(),
                TransactionService.class);
        PendingIntent pendingIntent = PendingIntent.getService(
                getApplicationContext(), 0, notificationIntent, 0);
        mgr.setRepeating(AlarmManager.RTC_WAKEUP,
                System.currentTimeMillis(), 4000, pendingIntent);

这是在设备处于活动状态且屏幕处于打开状态时调用方法的日志

this is the log of calling the method when device is active and screen is on

12-30 13:23:00.565 17397-17479/com.ids.simcardrefill D/url: calling
 12-30 13:23:04.565 17397-17537/com.ids.simcardrefill D/url:calling
 12-30 13:23:08.565 17397-17411/com.ids.simcardrefill D/url:calling
 12-30 13:23:12.565 17397-17655/com.ids.simcardrefill D/url:calling

这是设备休眠时方法的调用方式

and this is how the method is calling when device is sleeping

12-30 13:09:12.565 17397-17655/com.ids.simcardrefill D/url:calling
12-30 13:09:17.785 17397-17598/com.ids.simcardrefill D/url:calling
12-30 13:09:20.565 17397-17479/com.ids.simcardrefill D/url:calling
12-30 13:09:25.775 17397-17537/com.ids.simcardrefill D/url:calling
12-30 13:09:28.565 17397-17411/com.ids.simcardrefill D/url:calling

此处通话之间的区别是不准确的:2秒,5秒,3秒

here the difference between calling is inaccurate: 2 seconds, 5 seconds, 3 seconds

这是服务的外观:

public int onStartCommand(Intent intent, int flags, int startId) {


    mshared = PreferenceManager
            .getDefaultSharedPreferences(getApplicationContext());
    edit = mshared.edit();
    hostname = mshared.getString(
            getApplicationContext().getString(R.string.hostname), "0");
    contin = true;
    cost
   = mshared.getString(getString(R.string.test), "0.09");
            if (contin) {
                getTransactions get = new    getTransactions(getApplicationContext());
                get.execute(hostname);
            }

    return START_STICKY;
}

`

任何解决方案??

推荐答案

您应该创建一个在后台工作的服务: https://developer.android.com/guide/components/services.html

You should crate a service for working in background: https://developer.android.com/guide/components/services.html

您应使用Handler来实现every 4 second功能.

Handler handler = new Handler();
Runnable test = new Runnable() {
    @Override
    public void run() {
        //do work
        handler.post(test, 4000); //wait 4 sec and run again
    }
};

public void stopTest() {
    handler.removeCallbacks(test);
}

public void startTest() {
    handler.post(test,0); //wait 0 ms and run
}

我已经尝试过下面的代码,并且对我有用

i have tried the code below and it works for me

MyService.java

public class MyService extends Service {
    Handler handler;
    Runnable test;
    public MyService() {
        handler = new Handler();
        test = new Runnable() {
            @Override
            public void run() {
                Log.d("foo", "bar");
                handler.postDelayed(test, 100); //100 ms you should do it 4000
            }
        };

        handler.postDelayed(test, 0);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

AndroidManifest.xml

<service
    android:name=".MyService"
    android:enabled="true"
    android:exported="true"></service>

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    //some code
    startService(new Intent(this, MyService.class));
}

请记住,如果您想开始-停止功能在我的第一个示例中优先使用

And remember if you want start-stop functionality take loot at my first example.

这篇关于Android每4秒运行一次任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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