Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从android远程服务每隔30分钟运行一次方法_Android_Multithreading_Scheduled Tasks_Android Handler_Aidl - Fatal编程技术网

从android远程服务每隔30分钟运行一次方法

从android远程服务每隔30分钟运行一次方法,android,multithreading,scheduled-tasks,android-handler,aidl,Android,Multithreading,Scheduled Tasks,Android Handler,Aidl,我有一个在远程进程上运行的服务(通过AIDL接口)。这是一项不可阻挡的服务(从启动完成开始,一直持续到卸载应用程序)。此服务将继续侦听UDP套接字。我想每30分钟运行一次该服务中的函数(负责通过udp套接字向服务器发送ping消息) 我已经试着开始线程和睡眠30分钟,它没有工作 new Thread(new Runnable() { public void run() { while (true) { try { s

我有一个在远程进程上运行的服务(通过AIDL接口)。这是一项不可阻挡的服务(从启动完成开始,一直持续到卸载应用程序)。此服务将继续侦听UDP套接字。我想每30分钟运行一次该服务中的函数(负责通过udp套接字向服务器发送ping消息)

我已经试着开始线程和睡眠30分钟,它没有工作

new Thread(new Runnable() {
    public void run() {
        while (true) {
            try {
                sendPingMessage();
                Thread.currentThread().sleep(1000 * 60 * 30);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}).start();
此函数在任意时间内调用。不完全是在30分钟内

还尝试了计时器任务、处理程序和ScheduledExecutorService,但也没有成功

ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate (new Runnable() {
    public void run() {
        sendPingMessage();
    }
}, 0, 30, TimeUnit.MINUTES);
问题似乎出在android的深度睡眠模式上[

我也尝试过使用AlarmManger。但我无法在单独的类中使用AlarmManger的广播接收器。因为我想从android远程服务发送ping消息(我无法连接/绑定到广播接收器内的远程服务)


解决我的问题的最佳方法是什么?我可以在我的服务类中使用alarm Manager广播接收器吗(不使用单独的广播接收器类)?

我使用AlarmManager实现更新功能。也许这也适用于您

void updatecheck(Context context){

    Intent alarmIntent = new Intent(context, yourclass.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), 30 * 60 * 1000, pendingIntent);
}

问题是我不能在单独的类中使用广播接收器,因为我的ping发送功能在远程服务中,如果有办法在我的远程服务类中使用广播接收器,它会解决问题。也许处理广播意图?在广播接收器中启动服务,不要绑定它,绑定是不可能的不想在广播接收器内启动服务,服务已启动/正在运行(这是一项不可阻挡的服务)。我想连接到此服务以发送ping消息(ping消息通过服务发送)是的,我知道,唯一的方法是调用
startService
并在
onStartCommand
中获得通知,正如我所说的,您不能从广播接收器调用
bindService