android - 频繁的 Android 小部件更新

标签 android

问候,

我正在尝试编写一个显示大纪元时间的时钟小部件,我需要它每秒更新一次。目前我正在使用使用处理程序的服务来执行此操作:

 public class EpochService extends Service {

 private static final String TAG = "EpochService";

 // the time
 private static long mTime;

 // ui components
 private static RemoteViews mViews;
 private static ComponentName mComponent;
 private static AppWidgetManager mManager;

 // handler
 private static Handler mHandler = new Handler();

 @Override
 public void onStart(Intent intent, int id){
  Log.i(TAG, "start");

  // register the receiver
  IntentFilter filter = new IntentFilter();
  filter.addAction(Intent.ACTION_SCREEN_OFF);
  filter.addAction(Intent.ACTION_SCREEN_ON);
  registerReceiver(mIntentReceiver, filter);

  // set up ui
  mViews = new RemoteViews(this.getPackageName(), R.layout.main);
  mManager = AppWidgetManager.getInstance(this);
  mComponent = new ComponentName(this, Clock.class);

  // start
  mHandler.removeCallbacks(mEpochTimerTask);
  mHandler.postDelayed(mEpochTimerTask, 1000);
 }

 @Override
 public void onDestroy(){
  // unregister receiver to prevent memory leak
  mHandler.removeCallbacks(mEpochTimerTask);
  unregisterReceiver(mIntentReceiver);
 }

 @Override
 public IBinder onBind(Intent intent) {
  return null;
 }

 /**
  * The broadcast receiver - handles screen off events to stop drawing
  */
 private static BroadcastReceiver mIntentReceiver = new BroadcastReceiver(){
  @Override
  public void onReceive(Context context, Intent intent) {
   if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
    Log.i(TAG, "stop drawing");
    mHandler.removeCallbacks(mEpochTimerTask);
   }
   else {
    Log.i(TAG, "start drawing");
    mHandler.removeCallbacks(mEpochTimerTask);
    mHandler.postDelayed(mEpochTimerTask, 1000);
   }
  }
 };

 /**
  * Run the Epoch Timer
  */
 private static Runnable mEpochTimerTask = new Runnable(){
  @Override
  public void run() {
   Log.i(TAG, "run");
   mTime = System.currentTimeMillis() / 1000;

   mViews.setTextViewText(R.id.time, Long.toString(mTime));
   mManager.updateAppWidget(mComponent, mViews);
   mHandler.postAtTime(mEpochTimerTask, SystemClock.uptimeMillis() + 1000);
  }
 };

}

我遇到的问题是,随着时间的推移,我的主屏幕似乎变得明显滞后。我是 Android 的新手,所以我不确定我正在做的是否是正确的方法,或者是否有更好的方法来实现这一点(也许使用 AsyncTask?)。有没有一种方法可以频繁更新小部件而不会出现 UI 延迟?如果我需要发布更多代码,请告诉我。我的小部件只是启动和停止此服务,我的 appwidget-provder 有 android:updatePeriodMillis="0"。

在此先感谢您的帮助。

最佳答案

I'm trying to write a clock widget that displays Epoch time and I need it to update every second.

请不要。您的用户会用干草叉攻击您。每秒执行一次远程过程调用会大大降低设备速度并耗尽电池电量。

如果您想要主屏幕上的时钟每秒更新一次,请编写一个自定义主屏幕,这样它就可以在一个进程中完成。

The problem I'm having is that over time, my home screen appears to get noticeably laggy.

这是意料之中的。

Is there a way to update a widget frequently without suffering from UI lag?

没有。默认更新频率是每 30 分钟,而不是每秒。

关于android - 频繁的 Android 小部件更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2898632/

相关文章:

java - 添加第二个 TextView 替换 LinearLayout 中的第一个 TextView

android - 动态壁纸 : java. lang.IllegalStateException: Surface has already been released

java - Cling 库不定期触发 M 搜索请求

android - 在两个 fragment 之间添加一个监听器

java - 为 YQL 生成 URL

android - 从 Activity 绑定(bind)到服务或在不同进程中启动服务?

java - onProgressUpdate 未被调用 android

android - 是否可以获取操作栏中显示的菜单项数?

android - TextInputEditText 使用自定义设计下划线修复字符长度

android - 什么是 versionCodeMajor?和 versionCode 有什么区别?