package org.xutils.sample.download; import android.app.ActivityManager; import android.app.Service; import android.content.Context; import android.content.Intent; import android.os.IBinder; import org.xutils.db.converter.ColumnConverterFactory; import org.xutils.x; import java.util.List; /** * Author: wyouflf * Date: 13-11-10 * Time: 上午1:04 */ public class DownloadService extends Service { static { // 注册DownloadState在数据库中的值类型映射 ColumnConverterFactory.registerColumnConverter(DownloadState.class, new DownloadStateConverter()); } private static DownloadManager DOWNLOAD_MANAGER; public synchronized static DownloadManager getDownloadManager() { if (!DownloadService.isServiceRunning(x.app())) { Intent downloadSvr = new Intent(x.app(), DownloadService.class); x.app().startService(downloadSvr); } if (DownloadService.DOWNLOAD_MANAGER == null) { DownloadService.DOWNLOAD_MANAGER = DownloadManager.getInstance(); } return DOWNLOAD_MANAGER; } public DownloadService() { super(); } @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); } @Override public void onDestroy() { if (DOWNLOAD_MANAGER != null) { DOWNLOAD_MANAGER.stopAllDownload(); } super.onDestroy(); } public static boolean isServiceRunning(Context context) { boolean isRunning = false; ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningServiceInfo> serviceList = activityManager.getRunningServices(Integer.MAX_VALUE); if (serviceList == null || serviceList.size() == 0) { return false; } for (int i = 0; i < serviceList.size(); i++) { if (serviceList.get(i).service.getClassName().equals(DownloadService.class.getName())) { isRunning = true; break; } } return isRunning; } }