首页 > 解决方案 > 分片接口和后台服务的区别?

问题描述

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

这是我的服务方法:

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;
                    }
                }
            }
        }
    }
}

标签: javaandroidandroid-fragmentsandroid-service

解决方案


您可以编写一个您自己的接口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 }
    }
    //...
}

推荐阅读