/* * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.micode.notes.gtask.remote; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.AsyncTask; import net.micode.notes.R; import net.micode.notes.ui.NotesListActivity; import net.micode.notes.ui.NotesPreferenceActivity; // 定义一个继承自AsyncTask的类,用于异步执行任务,接收Void作为输入参数,String作为进度更新参数,Integer作为返回结果 public class GTaskASyncTask extends AsyncTask { // 定义一个静态常量,用作通知的ID private static int GTASK_SYNC_NOTIFICATION_ID = 5234235; // 定义一个接口,用于任务完成时的回调 public interface OnCompleteListener { void onComplete(); } // 声明一个Context对象,用于访问应用的资源和类 private Context mContext; // 声明一个NotificationManager对象,用于管理通知 private NotificationManager mNotifiManager; // 声明一个GTaskManager对象,用于管理任务(假设是一个自定义的任务管理类) private GTaskManager mTaskManager; // 声明一个OnCompleteListener对象,用于任务完成时的回调 private OnCompleteListener mOnCompleteListener; // 构造函数,初始化必要的对象 public GTaskASyncTask(Context context, OnCompleteListener listener) { mContext = context; // 初始化mContext mOnCompleteListener = listener; // 初始化mOnCompleteListener mNotifiManager = (NotificationManager) mContext .getSystemService(Context.NOTIFICATION_SERVICE); // 初始化mNotifiManager mTaskManager = GTaskManager.getInstance(); // 初始化mTaskManager,假设这是一个单例模式 } // 提供一个方法用于取消同步任务 public void cancelSync() { mTaskManager.cancelSync(); // 调用mTaskManager的cancelSync方法 } // 重写publishProgress方法,用于发布进度更新 public void publishProgess(String message) { publishProgress(new String[] { message }); // 调用父类的publishProgress方法,传递一个字符串数组 } // 私有方法,用于显示通知 private void showNotification(int tickerId, String content) { // 创建一个Notification对象,设置图标、标题和时间戳 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对象 // 根据tickerId判断点击通知后的跳转页面 if (tickerId != R.string.ticker_success) { pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, NotesPreferenceActivity.class), 0); } else { pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, NotesListActivity.class), 0); } // 设置通知的详细信息 notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content, pendingIntent); // 显示通知 mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification); } // 重写doInBackground方法,用于在后台线程执行任务 @Override protected Integer doInBackground(Void... unused) { // 发布进度更新,显示登录进度 publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity .getSyncAccountName(mContext))); // 执行同步任务,并返回结果 return mTaskManager.sync(mContext, this); } // 重写onProgressUpdate方法,用于在UI线程更新进度 @Override protected void onProgressUpdate(String... progress) { // 显示同步中的通知 showNotification(R.string.ticker_syncing, progress[0]); // 如果mContext是GTaskSyncService的实例,则发送广播 if (mContext instanceof GTaskSyncService) { ((GTaskSyncService) mContext).sendBroadcast(progress[0]); } } // 重写onPostExecute方法,用于在任务完成后在UI线程执行操作 @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)); } // 如果mOnCompleteListener不为空,则在新线程中调用onComplete方法 if (mOnCompleteListener != null) { new Thread(new Runnable() { public void run() { mOnCompleteListener.onComplete(); } }).start(); } } }