在 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))
有一个很好的开源服务器,它提供名为 Openfire 的 XMPP 集成。 ,我会推荐。
同一家公司还提供了一个名为 Smack 的客户端集成库,您可以在您的应用程序中轻松实现它以使用简单的聊天功能。
关于android - 在 android 中重复执行任务的最佳方法是什么? (例如 :- Refreshing scores, 更新用户界面),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19764228/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco