每30秒运行一次Android服务

时间:2017-04-03 09:18:04

标签: android service intervals

我正在使用独立于UI的Android服务创建通知。这完全没问题。以下是代码。

public class SendNotificationService extends Service {
    Context context;
    String test_heading;
    String test_body;

    final class notifThread implements Runnable {
        int service_id;

        notifThread(int service_id) {
            this.service_id = service_id;
        }

        @Override
        public void run() {
            String requested_method = "LoadBU";
            String bu_status = "1";

            CheckNewEntry checkNewEntry = new CheckNewEntry(SendNotificationService.this);
            checkNewEntry.execute(requested_method, bu_status);

            stopSelf(this.service_id);
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Thread thread = new Thread(new notifThread(startId));
        thread.start();

        return START_STICKY;
    }

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

    @Override
    public void onDestroy() {
        super.onDestroy();

        Toast.makeText(this, "Notifications Stopped...", Toast.LENGTH_LONG).show();
    }
}

此服务也会在系统启动时自动启动。 CheckNewEntry是我的AsyncTask,它检查数据库并在有任何更改时发送通知。我没有添加CheckNewEntry,因为它超出了这个问题的范围。

现在我想做的是,每隔30秒或1分钟运行CheckNewEntry

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

你可以使用这样的处理程序。

public class SendNotificationService extends Service {
        Context context;
        String test_heading;
        String test_body;
        public static Runnable runn;
        public static Handler hand =new Handler();
        final class notifThread implements Runnable {
            int service_id;

            notifThread(int service_id) {
                this.service_id = service_id;
            }

            @Override
            public void run() {
                String requested_method = "LoadBU";
                String bu_status = "1";

                CheckNewEntry checkNewEntry = new CheckNewEntry(SendNotificationService.this);

     runn = new Runnable() {
                @Override
                public void run() {

                     checkNewEntry.execute(requested_method, bu_status);

                    hand.postDelayed(runn, 30000);
                }
            };


            runn.run();


            stopSelf(this.service_id);
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Thread thread = new Thread(new notifThread(startId));
        thread.start();

        return START_STICKY;
    }

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

    @Override
    public void onDestroy() {
        super.onDestroy();

        Toast.makeText(this, "Notifications Stopped...", Toast.LENGTH_LONG).show();
    }
}

答案 1 :(得分:0)

在完成不同的Stackoverflow问题/答案后,我设法找到了自己的解决方案。

以下是我创建并正在运行的代码。

    public class SendNotificationService extends Service {
    public Context context = this;
    public Handler handler = null;
    public static Runnable runnable = null;

    @Override
    public void onCreate() {
        handler = new Handler();
        runnable = new Runnable() {
            public void run() {
                String requested_method = "LoadBU";
                String bu_status = "1";

                CheckNewEntry checkNewEntry = new CheckNewEntry(SendNotificationService.this);
                checkNewEntry.execute(requested_method, bu_status);

                handler.postDelayed(runnable, 10000);
            }
        };

        handler.postDelayed(runnable, 15000);
    }

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

        return START_STICKY;
    }

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

    @Override
    public void onDestroy() {
        handler.removeCallbacks(runnable);

        Toast.makeText(this, "Notifications Stopped...", Toast.LENGTH_LONG).show();
    }
}

如果有人能提供更好的解决方案,请发帖。