2 aaron 2014 Aaron_2014 于 2014.07.29 11:53 提问

关于Service的一些疑问

我现在做的一个程序有下载功能,下载是在service中去做的,为了跟Activity进行交互,我把service放到Application中:
public class FlyThemeApplication extends Application {

public static final String TAG_LOG = "FlyThemeApplication";

public FlyThemeDownloadService mDownloadService;

@Override
public void onCreate() {
    super.onCreate();
    Log.i(TAG_LOG, "FlyThemeApplication onCreate");

    Intent startServiceIntent = new Intent();
    startServiceIntent.setClass(this, FlyThemeDownloadService.class);
    startService(startServiceIntent);

    Log.i(TAG_LOG, "service.obj=" + mDownloadService);

}

然后在Service的onCreate方法中:mFlyThemApplication = (FlyThemeApplication) this.getApplication();
mFlyThemApplication.mDownloadService = this;

之后所有地方都是通过拿到Application,然后调用service中的方法:
mFlyThemeApplication.mDownloadService.startDownload(mFlyTheme);

请问这样做可行么?

1个回答

wangyaninglm
wangyaninglm   Ds   Rxr 2015.01.20 11:40
Csdn user default icon
上传中...
上传图片
插入图片