分片接口和后台服务的区别?
Differentiate between fragment interface and background 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;
}
}
}
}
}
}
您可以编写一个接口,让您的 Service
和 Fragment
都实现它,然后检查 class 的实例,然后做任何您需要做的事情。例如:
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 }
}
//...
}
我在服务文件中有一个方法,它的作用是检查我的服务器中是否有新的更新。这个方法是从两个地方调用的。它在应用程序启动时自动调用并且它从片段界面手动调用。 我想要的是 -> 当从用户界面调用方法时,我想在某些异常中显示警报,而不是将它们写入我的日志文件。
这是我的服务方式:
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
都实现它,然后检查 class 的实例,然后做任何您需要做的事情。例如:
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 }
}
//...
}