Delete 'GTaskSyncService.java'

Tanshuhan
pcoifxtab 6 months ago
parent e01f8e9e82
commit 50288ef416

@ -1,198 +0,0 @@
/*
* 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.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
/*
* GTaskSyncServiceGoogle
* Service
*/
public class GTaskSyncService extends Service {
public final static String ACTION_STRING_NAME = "sync_action_type";
public final static int ACTION_START_SYNC = 0;
public final static int ACTION_CANCEL_SYNC = 1;
public final static int ACTION_INVALID = 2;
public final static String GTASK_SERVICE_BROADCAST_NAME = "net.micode.notes.gtask.remote.gtask_sync_service";
public final static String GTASK_SERVICE_BROADCAST_IS_SYNCING = "isSyncing";
public final static String GTASK_SERVICE_BROADCAST_PROGRESS_MSG = "progressMsg";
private static GTaskASyncTask mSyncTask = null;
private static String mSyncProgress = "";
/*
*
*/
private void startSync() {
// 如果同步任务尚未启动
if (mSyncTask == null) {
// 创建一个新的GTaskASyncTask对象
mSyncTask = new GTaskASyncTask(this, new GTaskASyncTask.OnCompleteListener() {
public void onComplete() {
// 同步任务完成后将mSyncTask置为null
mSyncTask = null;
// 发送广播通知同步完成
sendBroadcast("");
// 停止服务
stopSelf();
}
});
// 发送广播通知同步开始
sendBroadcast("");
// 执行同步任务
mSyncTask.execute();
}
}
/*
*
*/
private void cancelSync() {
// 如果同步任务正在执行
if (mSyncTask != null) {
// 调用cancelSync方法取消同步
mSyncTask.cancelSync();
}
}
/*
*
*/
@Override
public void onCreate() {
// 将同步任务置为null
mSyncTask = null;
}
/*
*
* @param intent
* @param flags
* @param startId ID
* @return
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 获取意图中的额外数据
Bundle bundle = intent.getExtras();
// 如果额外数据不为空且包含ACTION_STRING_NAME
if (bundle != null && bundle.containsKey(ACTION_STRING_NAME)) {
// 根据ACTION_STRING_NAME的值执行相应的操作
switch (bundle.getInt(ACTION_STRING_NAME, ACTION_INVALID)) {
case ACTION_START_SYNC:
// 启动同步操作
startSync();
break;
case ACTION_CANCEL_SYNC:
// 取消同步操作
cancelSync();
break;
default:
break;
}
// 返回START_STICKY表示服务在异常终止后会自动重启
return START_STICKY;
}
// 如果额外数据为空或不包含ACTION_STRING_NAME调用父类的onStartCommand方法
return super.onStartCommand(intent, flags, startId);
}
/*
*
*/
@Override
public void onLowMemory() {
// 如果同步任务正在执行
if (mSyncTask != null) {
// 调用cancelSync方法取消同步
mSyncTask.cancelSync();
}
}
/*
* null
* @param intent
* @return null
*/
public IBinder onBind(Intent intent) {
// 返回null表示不支持绑定服务
return null;
}
/*
* 广
* @param msg
*/
public void sendBroadcast(String msg) {
// 更新同步进度字符串
mSyncProgress = msg;
// 创建一个新的Intent对象用于发送广播
Intent intent = new Intent(GTASK_SERVICE_BROADCAST_NAME);
// 添加是否正在同步的标志
intent.putExtra(GTASK_SERVICE_BROADCAST_IS_SYNCING, mSyncTask != null);
// 添加进度消息
intent.putExtra(GTASK_SERVICE_BROADCAST_PROGRESS_MSG, msg);
// 发送广播
sendBroadcast(intent);
}
/*
*
* @param activity Activity
*/
public static void startSync(Activity activity) {
// 设置活动上下文
GTaskManager.getInstance().setActivityContext(activity);
// 创建一个新的Intent对象用于启动服务
Intent intent = new Intent(activity, GTaskSyncService.class);
// 添加启动同步的命令
intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_START_SYNC);
// 启动服务
activity.startService(intent);
}
/*
*
* @param context
*/
public static void cancelSync(Context context) {
// 创建一个新的Intent对象用于启动服务
Intent intent = new Intent(context, GTaskSyncService.class);
// 添加取消同步的命令
intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_CANCEL_SYNC);
// 启动服务
context.startService(intent);
}
/*
*
* @return truefalse
*/
public static boolean isSyncing() {
// 如果mSyncTask不为null表示正在同步
return mSyncTask != null;
}
/*
*
* @return
*/
public static String getProgressString() {
// 返回当前的同步进度字符串
return mSyncProgress;
}
}
Loading…
Cancel
Save