From 49b3be71392fb9724ba74f617480341ea566dcf3 Mon Sep 17 00:00:00 2001 From: cnz <2869874844@qq.com> Date: Mon, 19 May 2025 17:15:51 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A2=81=E5=93=B2=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../notes/gtask/remote/GTaskASyncTask.java | 107 +++++++++++++----- 1 file changed, 81 insertions(+), 26 deletions(-) diff --git a/src/net/micode/notes/gtask/remote/GTaskASyncTask.java b/src/net/micode/notes/gtask/remote/GTaskASyncTask.java index b3b61e7..cb14b35 100644 --- a/src/net/micode/notes/gtask/remote/GTaskASyncTask.java +++ b/src/net/micode/notes/gtask/remote/GTaskASyncTask.java @@ -1,4 +1,3 @@ - /* * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) * @@ -15,7 +14,9 @@ * limitations under the License. */ -package net.micode.notes.gtask.remote; + //这个类封装了Google任务同步的所有逻辑,包括后台执行、进度通知、错误处理和结果反馈,为应用提供了完整的同步功能实现。 + +package net.micode.notes.gtask.remote;//导入包 import android.app.Notification; import android.app.NotificationManager; @@ -28,23 +29,32 @@ import net.micode.notes.R; import net.micode.notes.ui.NotesListActivity; import net.micode.notes.ui.NotesPreferenceActivity; - +/** + * Google任务同步的异步任务类,继承自AsyncTask + * 用于在后台执行同步操作并显示通知 + */ public class GTaskASyncTask extends AsyncTask { + // 同步通知的唯一ID private static int GTASK_SYNC_NOTIFICATION_ID = 5234235; + /** + * 同步完成回调接口 + */ public interface OnCompleteListener { void onComplete(); } - private Context mContext; - - private NotificationManager mNotifiManager; - - private GTaskManager mTaskManager; - - private OnCompleteListener mOnCompleteListener; + private Context mContext; // 上下文对象 + private NotificationManager mNotifiManager; // 通知管理器 + private GTaskManager mTaskManager; // Google任务管理器 + private OnCompleteListener mOnCompleteListener; // 完成监听器 + /** + * 构造函数 + * @param context 上下文 + * @param listener 完成监听器 + */ public GTaskASyncTask(Context context, OnCompleteListener listener) { mContext = context; mOnCompleteListener = listener; @@ -53,71 +63,116 @@ public class GTaskASyncTask extends AsyncTask { mTaskManager = GTaskManager.getInstance(); } + /** + * 取消同步操作 + */ public void cancelSync() { mTaskManager.cancelSync(); } + /** + * 发布进度更新 + * @param message 进度消息 + */ public void publishProgess(String message) { - publishProgress(new String[] { - message - }); + publishProgress(new String[] { message }); } + /** + * 显示通知 + * @param tickerId 通知标题资源ID + * @param content 通知内容 + */ private void showNotification(int tickerId, String content) { - Notification notification = new Notification(R.drawable.notification, mContext - .getString(tickerId), System.currentTimeMillis()); + // 创建通知对象 + Notification notification = new Notification(R.drawable.notification, + mContext.getString(tickerId), System.currentTimeMillis()); + + // 设置通知属性 notification.defaults = Notification.DEFAULT_LIGHTS; notification.flags = Notification.FLAG_AUTO_CANCEL; + + // 根据通知类型设置不同的PendingIntent PendingIntent pendingIntent; if (tickerId != R.string.ticker_success) { - pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, - NotesPreferenceActivity.class), 0); - + // 失败时跳转到设置页面 + pendingIntent = PendingIntent.getActivity(mContext, 0, + new Intent(mContext, NotesPreferenceActivity.class), 0); } else { - pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, - NotesListActivity.class), 0); + // 成功时跳转到笔记列表 + pendingIntent = PendingIntent.getActivity(mContext, 0, + new Intent(mContext, NotesListActivity.class), 0); } - notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content, - pendingIntent); + + // 设置通知内容 + notification.setLatestEventInfo(mContext, + mContext.getString(R.string.app_name), content, pendingIntent); + + // 显示通知 mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification); } + /** + * 后台执行同步任务 + * @param unused 未使用的参数 + * @return 同步结果状态码 + */ @Override protected Integer doInBackground(Void... unused) { - publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity - .getSyncAccountName(mContext))); + // 发布登录进度 + publishProgess(mContext.getString(R.string.sync_progress_login, + NotesPreferenceActivity.getSyncAccountName(mContext))); + + // 执行同步并返回结果 return mTaskManager.sync(mContext, this); } + /** + * 进度更新回调 + * @param progress 进度消息数组 + */ @Override protected void onProgressUpdate(String... progress) { + // 显示同步中的通知 showNotification(R.string.ticker_syncing, progress[0]); + + // 如果是服务上下文,发送广播 if (mContext instanceof GTaskSyncService) { ((GTaskSyncService) mContext).sendBroadcast(progress[0]); } } + /** + * 同步完成后回调 + * @param result 同步结果状态码 + */ @Override protected void onPostExecute(Integer result) { + // 根据不同的结果状态显示不同的通知 if (result == GTaskManager.STATE_SUCCESS) { + // 同步成功 showNotification(R.string.ticker_success, mContext.getString( R.string.success_sync_account, mTaskManager.getSyncAccount())); NotesPreferenceActivity.setLastSyncTime(mContext, System.currentTimeMillis()); } else if (result == GTaskManager.STATE_NETWORK_ERROR) { + // 网络错误 showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_network)); } else if (result == GTaskManager.STATE_INTERNAL_ERROR) { + // 内部错误 showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_internal)); } else if (result == GTaskManager.STATE_SYNC_CANCELLED) { + // 同步取消 showNotification(R.string.ticker_cancel, mContext .getString(R.string.error_sync_cancelled)); } + + // 调用完成监听器 if (mOnCompleteListener != null) { new Thread(new Runnable() { - public void run() { mOnCompleteListener.onComplete(); } }).start(); } } -} +} \ No newline at end of file