android - 在 android 中重复执行任务的最佳方法是什么? (例如 :- Refreshing scores, 更新用户界面)

标签 android chat alarmmanager timertask livechat

在 android 中有一些用于刷新处理的选项,例如 Timer、TimerTask、ScheduledExecutorService、AlarmManager 和 Handler。这是执行此操作的最佳方法。

有没有人检查过上述方法的资源利用率?。我在这里列出了上述方法的实现。

使用处理程序重复执行任务

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {

    public void run() {
        new MyScheduledTask.execute(param);
    }

}, TimeInterval);

使用 Timer 重复执行任务

timer = new Timer();

timer.scheduleAtFixedRate(new TimerTask() {

    synchronized public void run() {
        new MyScheduledTask.execute(param);
        }

}}, 10000, 10000);

使用 ScheduledExecutorService 重复执行任务

ScheduledExecutorService scheduler =
Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate
  (new Runnable() {
     public void run() {
        new MyScheduledTask.execute(param);
     }
  }, 0, 10, TimeInterval);

使用 Timer 和 TimerTask 重复执行任务

Timer timer = new Timer();
timer.schedule(new UpdateTimeTask(),1, TimeInterval);
class UpdateTimeTask extends TimerTask {

    public void run() 
       {        
        new MyScheduledTask.execute(param);
       }
}

用于执行计划任务的AlarmManager

public void setupTask(){

    // check task is scheduled or not
    boolean alarmUp = (PendingIntent.getBroadcast(this, 0, 
            new Intent("YourPackageHere.AlarmReceiver"), 
            PendingIntent.FLAG_NO_CREATE) != null);

    if (  !alarmUp) {
        Intent intent = new Intent("YourPackageHere.AlarmReceiver");
        intent.putExtra("activate", true);
        PendingIntent pendingIntent =
                    PendingIntent.getBroadcast(this, 0, 
                  intent, PendingIntent.FLAG_UPDATE_CURRENT);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 0);   
        calendar.set(Calendar.MINUTE, 1);
        calendar.set(Calendar.SECOND, 0);

        AlarmManager alarmManager =
                    (AlarmManager)
                    this.getSystemService(this.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                    calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
                    pendingIntent);

        calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 7);   
        calendar.set(Calendar.MINUTE, 0);

        alarmManager = (AlarmManager)
                    this.getSystemService(this.ALARM_SERVICE);
        PendingIntent pendingIntent2 =
                    PendingIntent.getBroadcast(this, 1, 
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                    calendar.getTimeInMillis(),
                    AlarmManager.INTERVAL_DAY, pendingIntent2);         

    }

}

报警管理器类

public class AlarmReceiver extends BroadcastReceiver { 

@Override
public void onReceive(Context context, Intent intent) {

    if (intent.hasExtra("activate")) {

        new MyScheduledTask.execute(param);
    }

}
}

list

<receiver android:name="YourPackageHere.AlarmReceiver"></receiver>

最佳答案

要持续在线,您需要一个在线程之上运行的 android Service。 您可以使用您在服务中声明的任何上述方法。

但是当您制作聊天应用程序时,您必须每 2-3 秒连续访问一次服务器,我认为这对用户不利(就您的应用程序将使用的互联网数据而言)。

用于聊天应用程序的最佳推荐协议(protocol)是 XMPP( Jabber )。它定义了一个普通聊天应用程序应该具备的所有规则,并且非常容易实现。 它是一种推送通知类型的服务器,每当有新消息到达或添加新 friend 时,它会自动向客户端推送通知。(即使Gtalk也使用此协议(protocol))

有一个很好的开源服务器,它提供名为 OpenfireXMPP 集成。 ,我会推荐。

同一家公司还提供了一个名为 Smack 的客户端集成库,您可以在您的应用程序中轻松实现它以使用简单的聊天功能。

关于android - 在 android 中重复执行任务的最佳方法是什么? (例如 :- Refreshing scores, 更新用户界面),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19764228/

相关文章:

forms - 如何锁定在 Delphi 中的克隆表单?

node.js - 在实时聊天中管理多个客户端之间的消息传递的最佳方法是什么?

java - 立即显示 Activity 而不是显示通知

Android - colors.xml 资源到 int 值

android - 如何在不崩溃的情况下使用 Android NDK 在线程上插入断点?

ios - iOS swift 3.0 中 XMPP 的解决方案

android - 我想每 15 分钟向服务器发布一次位置更新,即使该应用程序未在前台运行

android - AlarmManager的RTC和RTC_WAKEUP有什么区别

android - 如何使 Snackbar 不与 Android 导航控件重叠

android - 在模块 classes.jar com.google.android.gms :play-services-measurement-impl: 中发现重复的类 com.google.android.gms.internal.measurement.zzdu