【问题标题】:Run Android Service every 30 seconds每 30 秒运行一次 Android 服务
【发布时间】:2017-04-03 09:18:04
【问题描述】:

我正在使用独立于 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

谁能帮忙?

【问题讨论】:

标签: android service intervals


【解决方案1】:

在经历了不同的 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();
    }
}

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 我现在就去试试。
    • 无法执行任务:任务已经执行(一个任务只能执行一次)
    • 我更喜欢你的答案,如果它有效,因为它不会改变我的代码。但是我自己想出的答案也发布了,这是有效的。
    猜你喜欢
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 2015-01-18
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    相关资源
    最近更新 更多