189 8069 5689

Android8.1Launcher3实现动态指针时钟功能-创新互联

本文主要实现功能,可能有不合理的地方

创新互联建站是一家专业提供秦安企业网站建设,专注与成都网站制作、网站建设、外贸网站建设H5开发、小程序制作等业务。10年已为秦安众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。

首先创建一个实现功能的工具里,直接上代码:

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import com.android.launcher3.ItemInfo;
import com.android.launcher3.LauncherSettings;
import com.android.launcher3.ShortcutInfo;
import com.android.launcher3.util.LogUtil;
public class DeskClockUtil {
  private OnDeskClockIconChangeListener mListener;
  private ItemInfo mItemInfo;
  private boolean mIsResume;
  private Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
      super.handleMessage(msg);
      if (msg.what == 100) {
        Message msg1 = new Message();
        msg1.what = 100;
        msg1.obj = msg.obj;
        mHandler.sendMessageDelayed(msg1, 60000);
        if (mListener != null && mItemInfo != null) {
          mListener.onChange(IconUtil.getDeskClockIcon((Context) msg.obj), mItemInfo);
        }
      }
    }
  };
  private static DeskClockUtil sInstance;
  private DeskClockUtil() {
  }
  public static DeskClockUtil getInstance() {
    if (sInstance == null) {
      sInstance = new DeskClockUtil();
    }
    return sInstance;
  }
  private void refresh(Context context) {
    if (mListener != null && mItemInfo != null) {
      mListener.onChange(IconUtil.getDeskClockIcon(context), mItemInfo);
    }
    if (mHandler.hasMessages(100)) {
      mHandler.removeMessages(100);
    }
    Message msg = new Message();
    msg.what = 100;
    msg.obj = context;
    mHandler.sendMessageDelayed(msg,
        1000 * (60 - Integer.parseInt(DateUtils.getCurrentSecond())));
  }
  public void onResume(Context context) {
    mIsResume = true;
    refresh(context);
  }
  public void onPause() {
    mIsResume = false;
    mHandler.removeMessages(100);
  }
  public void setListener(OnDeskClockIconChangeListener listener, ItemInfo info, Context context) {
    if (!(info instanceof ShortcutInfo)) {
      return;
    }
    String pkg = null;
    if (info.getIntent() != null && info.getIntent().getComponent() != null) {
      pkg = info.getIntent().getComponent().getPackageName();
    }
    if (!"com.android.deskclock".equals(pkg) || info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
      return;
    }
    mListener = listener;
    mItemInfo = info;
    if (mIsResume) {
      refresh(context);
    }
  }
  public interface OnDeskClockIconChangeListener {
    void onChange(Bitmap icon, ItemInfo info);
  }
}

网站题目:Android8.1Launcher3实现动态指针时钟功能-创新互联
转载来源:http://jkwzsj.com/article/cceics.html

其他资讯