片段接口和后台服务有何区别?

时间:2018-10-09 14:45:16

标签: java android android-fragments android-service

我在服务文件中有一个方法,它的作用是检查服务器中是否有新更新。.从两个地方调用此方法..应用启动时自动调用该方法,从片段接口手动进行。 我想要的是->当从用户界面调用的方法要在某些异常中显示警报,而不是将其写入日志文件中。

这是我的服务方式:

public void checkUpdate(Boolean IsUpdateExist,Boolean IsServerError,String LatestApplicationCode,
                        String LatestApplicationName,Integer LatestVersionCode,String LatestVersionName,
                        String ResponseUpdateInformationType,String ResponseUpdateURI,String ServerErrorMessage,String ServerErrorStackTrace){

    if(IsServerError == null){
        //If called from a user interface => show alert dialog here.

        FileUtil.logInformation(this.getClass().getName(), "Can't connect to server!");
        return;
    }

    else{
        if(IsServerError){
        //If called from a user interface => show alert dialog here.

            FileUtil.logInformation(this.getClass().getName(), "Server error! | error message: "+ServerErrorMessage);
            return;
        }
        else {
            if(!IsUpdateExist || IsUpdateExist == null){
            //If called from a user interface => show alert dialog here.

                FileUtil.logInformation(this.getClass().getName(), "No updates available !");
                return;
            }
            else {
                if (LatestVersionCode != null && LatestVersionCode <= CurrentVersionCode){
               //If called from a user interface => show alert dialog here.

                    FileUtil.logInformation(this.getClass().getName(), "No new updates ! | version in the server= "+LatestVersionCode
                            +" version installed= "+CurrentVersionCode);
                    return;
                }
                else {
                    if(ResponseUpdateURI != null && !ResponseUpdateURI.contentEquals("")){
                        this.updateApp(IsUpdateExist,IsServerError,LatestApplicationCode,LatestApplicationName,LatestVersionCode,
                                LatestVersionName,ResponseUpdateInformationType,ResponseUpdateURI,ServerErrorMessage,ServerErrorStackTrace);
                    }
                    else {
                    //If called from a user interface => show alert dialog here.

                        FileUtil.logInformation(this.getClass().getName(), "It seems there is a problem with the download URL");
                        return;
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以编写一个接口,ServiceFragment都可以实现该接口,然后检查该类的实例,然后执行所需的任何操作。例如:

interface MyCustomJobExecutor {}

public class MyFragment extends Fragment implements MyCustomJobExecutor {/*...*/}

public class MyService extends Service implements MyCustomJobExecutor {
    //...
    public void checkUpdate(MyCustomJobExecutor jobExecutor, ...) {
        if (jobExecutor instanceof MyFragment) {
            // Manual code execution here
        } else { // Automatic code execution stuff }
    }
    //...
}