运行 Android 每 30 秒服务一次

Run Android Service every 30 seconds

我正在使用独立于 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 因为它超出了这个问题的范围。

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

有人能帮忙吗?

您可以像这样使用处理程序。

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();
    }
}

在经历了不同的 Whosebug questions/answers 之后,我设法想出了自己的解决方案。

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

    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();
    }
}

如果你们中的任何人可以提供更好的解决方案,请post。