[英]How to notify when a method has finished
How can i notify and execute instantly a method from the MainActivity
class when a method from a non-activity class has finished?当来自非活动 class 的方法完成时,我如何通知并立即执行
MainActivity
class 的方法?
MainActivity class:主要活动 class:
public class MainActivity extends AppCompatActivity {
private ExpandableListViewAdapter mExpandableListViewAdapter;
PreselectionAplicationUseCases preselectionAplicationUseCases;
public void Preselection(){
if(preselectionAplicationUseCases.isMsg100PreselectionAplication()) {
mExpandableListViewAdapter.setSelectedChild(-1);
}
}
Non-activity class:非活动 class:
public class MSG0100 implements PreselectionAplicationUseCases {
msg100PreselectionAplication=true;
}
public boolean isMsg100PreselectionAplication() {
return msg100PreselectionAplication;
}
public void setMsg100PreselectionAplication(boolean msg100PreselectionAplication) {
this.msg100PreselectionAplication = msg100PreselectionAplication;
}
And the interface:和界面:
public interface PreselectionAplicationUseCases {
boolean isMsg100PreselectionAplication();
void setMsg100PreselectionAplication(boolean msg100PreselectionAplication);
}
So how can I execute the method Preselection
automatically when this happens msg100PreselectionAplication=true;
那么当发生这种情况时,如何自动执行方法
Preselection
msg100PreselectionAplication=true;
in the non-activity class.在非活动 class。 I was thinking that i can use a thread with wait and notify, but i think that can give some problems to the UI thread.
我在想我可以使用带有等待和通知的线程,但我认为这会给 UI 线程带来一些问题。
Couldn't you just use Observer pattern?你不能只使用观察者模式吗?
public class MSG0100 implements PreselectionAplicationUseCases {
private OnMsg100PreselectionChanged listener = null;
public void setOnMsgPreselectionChanged(OnMsg100PreselectionChanged listener) {
this.listener = listener;
}
public void setMsg100PreselectionAplication(boolean msg100PreselectionAplication) {
if(listener != null) {
listener.onPreselectionChanged(msg100PreselectionAplication);
}
}
}
interface OnMsg100PreselectionChanged {
void onPreselectionChanged(boolean isChanged);
}
public class MainActivity extends AppCompatActivity {
private ExpandableListViewAdapter mExpandableListViewAdapter;
PreselectionAplicationUseCases preselectionAplicationUseCases;
public void Preselection(){
preselectionApplicationUseCases.setOnMsgPreselectionChanged(new OnMsg100PreselectionChanged {
@Override
void onPreselectionChanged(boolean isChanged) {
//do something with changed boolean
}
});
}
}
You can create inteface like this and implement it in your activity class:您可以像这样创建接口并在您的活动 class 中实现它:
public interface OnMethodFinishedListener {
void onMethodFinish();
}
then make method call in your non-activity class:然后在您的非活动 class 中进行方法调用:
public class MSG0100 implements PreselectionAplicationUseCases {
msg100PreselectionAplication=true;
}
private OnMethodFinishedListener mListener
public MSG0100 (OnMethodFinishedListener listner){
mListener = listener;
}
public boolean isMsg100PreselectionAplication() {
return msg100PreselectionAplication;
}
public void setMsg100PreselectionAplication(boolean msg100PreselectionAplication) {
this.msg100PreselectionAplication = msg100PreselectionAplication;
//here for example. or anywhere you need
mListener.onMethodFinish
}
And that is it - your activity will get call in overrided onMethodFinished method就是这样 - 您的活动将在覆盖的 onMethodFinished 方法中调用
This could be done also using while这也可以使用 while
while(msg100PreselectionAplication=true){
//do something to notify after that we set preselection again to false
msg100PreselectionAplication=false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.