ADD file via upload

master
prw4s76gk 11 months ago
parent 0805952ffa
commit 65e58d061f

@ -0,0 +1,216 @@
/**
* @ProiectName:MiNote
* @Package: gtask.remote
* @ClassName: GTaskASyncTask
* @Description:GTaskASyncTaskUI线UI
* 1.线UI线UI
* 2.GTaskASyncTaskpublishProgress()
* UI线onProgressUpdate()便UI
* 3.GTaskASyncTaskUI线UI线onPostExecute()
* UI
* @Author: wmq
*/
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;
/**
* @: GTaskASyncTask
* @ :
* mContext
* mNotifiManager
* mTaskManagerGTaskManager
* mOnCompleteListener
* @:GTaskASyncTask
* 使
*
* @:
* @Author: wmq
*/
public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {//执行的任务可以接收Void类型的输入参数发布String类型的进度信息最终返回Integer类型的结果。
private static int GTASK_SYNC_NOTIFICATION_ID = 5234235;//静态变量用于标识通知的ID。通常用于发送通知时唯一标识通知的标识符。
public interface OnCompleteListener {//内部接口定义了一个回调方法onComplete()。当任务完成时,可以通过实现该接口,在任务完成时进行回调通知。
void onComplete();
}
private Context mContext;
private NotificationManager mNotifiManager;
private GTaskManager mTaskManager;
private OnCompleteListener mOnCompleteListener;
/**
* @: GTaskASyncTask
* @ : context/listener
* @: )
* @:
* @Author: wmq
*/
public GTaskASyncTask(Context context, OnCompleteListener listener) {
mContext = context; //将传入的OnCompleteListener接口的实例赋值给成员变量mOnCompleteListener。用于在任务完成时回调通知调用者。
mOnCompleteListener = listener;
mNotifiManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);//通过上下文对象获取通知管理器的实例。该通知管理器用于发送通知。
mTaskManager = GTaskManager.getInstance();//通过调用GTaskManager类的静态方法getInstance()获取GTaskManager的单例实例。mTaskManager用于管理任务的执行。
}
/**
* @: cancelSync()
* @ :
* @:
* @: mTaskManagercancelSync()
*
* @Author: wmq
*/
public void cancelSync() {
mTaskManager.cancelSync();
}
/**
* @: publishProgess
* @ :message
* @: ,
* publishProgress()UI线onProgressUpdate()
* UI
* @: publishProgessAsyncTaskpublishProgress()
* messageGTaskASyncTask
* publishProgess()
* @Author: wmq
*/
public void publishProgess(String message) {// 发布进度单位系统将会调用onProgressUpdate()方法更新这些值
publishProgress(new String[] {
message
});
}
/* private void showNotification(int tickerId, String content) {
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;
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);
notification.contentIntent = pendingIntent;
mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);
}*/
/**
* @: showNotification
* @ :int tickerId / String content
* @:
* tickerId
* 使Notification.Builder
* builder.getNotification()
* 使mNotifiManager.notify()
* @: tickerIdR.string.ticker_success
* NotesPreferenceActivityPendingIntenttickerIdR.string.ticker_success
* NotesListActivityPendingIntent
* @Author: wmq
*/
private void showNotification(int tickerId, String content) {
PendingIntent pendingIntent;
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.Builder builder = new Notification.Builder(mContext)
.setAutoCancel(true) //设置通知点击后自动取消
.setContentTitle(mContext.getString(R.string.app_name))//设置通知的标题为应用名称。
.setContentText(content)//设置通知的内容为传入的content参数。
.setContentIntent(pendingIntent)//设置通知点击时的跳转意图为之前创建的PendingIntent对象。
.setWhen(System.currentTimeMillis())//时间设置
.setOngoing(true);//用户无法手动清除
Notification notification=builder.getNotification();
mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);//将通知显示在系统通知栏中。其中GTASK_SYNC_NOTIFICATION_ID作为通知的ID可以用于标识该通知。
}
/**
* @: doInBackground
* @ : unused
* @:
* @: @OverrideAsyncTask
*publishProgress()
*
* @Author: wmq
*/
@Override
protected void onProgressUpdate(String... progress) {
showNotification(R.string.ticker_syncing, progress[0]);
if (mContext instanceof GTaskSyncService) {
((GTaskSyncService) mContext).sendBroadcast(progress[0]);
}
}
/**
* @: onPostExecute
* @ : result
* @: UI
* @: result
* @Author: wmq
*/
@Override //表示该方法是对父类AsyncTask中的方法进行重写
protected void onPostExecute(Integer result) { //这表示该方法接收一个Integer类型的参数result表示后台操作的结果。
if (result == GTaskManager.STATE_SUCCESS) { //表示同步操作成功那么会显示一个通知更新上次同步时间并执行NotesPreferenceActivity.setLastSyncTime()方法保存最新的同步时间。
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) { //表示内部错误会显示一个通知提示同步时发生内部错误。如果result等于
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) { //如果存在mOnCompleteListener回调函数对象会在新的线程中执行mOnCompleteListener.onComplete()方法。
new Thread(new Runnable() {
public void run() {
mOnCompleteListener.onComplete();
}
}).start();
}
}
}
Loading…
Cancel
Save