java - 分片接口和后台服务的区别?
问题描述
我在服务文件中有一个方法,它的作用是检查我的服务器中是否有新的更新。这个方法是从两个地方调用的。它在应用程序启动时自动调用,并从一个手动调用片段接口。我想要的是->当从用户界面调用的方法时,我想在某些异常中显示警报,而不是将它们写入我的日志文件中。
这是我的服务方法:
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;
}
}
}
}
}
}
解决方案
您可以编写一个您自己的接口Service
并Fragment
实现它,然后检查该类的实例,然后做任何您需要做的事情。例如:
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 }
}
//...
}
推荐阅读
- python-3.x - 不使用 numpy 的矩阵中所有对角线的列表 - Python 3
- android - 对一个元素的查询是否可能导致 .CursorWindowAllocationException?
- r - 根据汇总计数计算序列
- vba - 根据另一个字段的值启用/禁用对某个记录字段的输入,但在同一记录
- powerbi - 有没有办法在 DAX 中按类别创建和显示运行总计?
- java - 如何修复泛型类型参数的运行时“空指针异常”
- certificate - VLC chromecast cast 在不受信任的证书上失败
- c - 如果条件读取如何?
- javascript - 使用嵌套的 javascript 对象返回空对象
- wordpress - WordPress 滑块小部件重新定位网页