Java Android:如何从自定义方法获取回调?
我有以下课程:Java Android:如何从自定义方法获取回调?,java,android,Java,Android,我有以下课程: public class TimerTaskHelper { private static TimerTaskHelper instance = null; private static Integer ONE_MINUTE = 60000; private static Handler handler; private static Runnable runnableInstance; private static Container
public class TimerTaskHelper {
private static TimerTaskHelper instance = null;
private static Integer ONE_MINUTE = 60000;
private static Handler handler;
private static Runnable runnableInstance;
private static Container container;
public static TimerTaskHelper getInstance(Container c) {
if (instance == null) {
instance = new TimerTaskHelper(c);
}
return instance;
}
public TimerTaskHelper(Container c) {
this.handler = new Handler();
this.runnableInstance = runnableCode;
this.container = c;
// Kick off the first runnableInstance task right away
handler.post(runnableCode);
}
// Define the task to be run here
private Runnable runnableCode = new Runnable() {
@Override
public void run() {
// Do something here on the main thread
Logger.d("Called");
// Repeat this runnableInstance code again every 2 seconds
handler.postDelayed(runnableCode, 2000);
container.notifyObservers();
}
};
public void stopExecution() {
handler.removeCallbacks(runnableInstance);
instance = null;
}
}
我可以使用以下命令从控制器获取实例:
mTimerTaskHelper = TimerTaskHelper.getInstance(container);
但我想在每次测试之后在控制器中得到回调
private Runnable runnableCode = new Runnable() {
@Override
public void run() {
及
来自控制器
我怎样才能用最好的方式来做呢?我想你想要这样的东西
interface CallBack {
void callBackMethod();
}
public class TimerTaskHelper {
private static TimerTaskHelper instance = null;
private static Integer ONE_MINUTE = 60000;
private static Handler handler;
private static Runnable runnableInstance;
private static Container container;
private CallBack callback;
public void setCallBack(CallBack cb){
callback=cb;
}
public static TimerTaskHelper getInstance(Container c) {
if (instance == null) {
instance = new TimerTaskHelper(c);
}
return instance;
}
public TimerTaskHelper(Container c) {
this.handler = new Handler();
this.runnableInstance = runnableCode;
this.container = c;
// Kick off the first runnableInstance task right away
handler.post(runnableCode);
}
// Define the task to be run here
private Runnable runnableCode = new Runnable() {
@Override
public void run() {
// Do something here on the main thread
Logger.d("Called");
// Repeat this runnableInstance code again every 2 seconds
handler.postDelayed(runnableCode, 2000);
container.notifyObservers();
if(callback!=null){
callback.callBackMethod();
}
};
mTimerTaskHelper = TimerTaskHelper.getInstance(container);
mTimerTaskHelper.setCallBack(new CallBack(){
void callBackMethod(){
//TODO your code
}});
我想你想要这样的东西
interface CallBack {
void callBackMethod();
}
public class TimerTaskHelper {
private static TimerTaskHelper instance = null;
private static Integer ONE_MINUTE = 60000;
private static Handler handler;
private static Runnable runnableInstance;
private static Container container;
private CallBack callback;
public void setCallBack(CallBack cb){
callback=cb;
}
public static TimerTaskHelper getInstance(Container c) {
if (instance == null) {
instance = new TimerTaskHelper(c);
}
return instance;
}
public TimerTaskHelper(Container c) {
this.handler = new Handler();
this.runnableInstance = runnableCode;
this.container = c;
// Kick off the first runnableInstance task right away
handler.post(runnableCode);
}
// Define the task to be run here
private Runnable runnableCode = new Runnable() {
@Override
public void run() {
// Do something here on the main thread
Logger.d("Called");
// Repeat this runnableInstance code again every 2 seconds
handler.postDelayed(runnableCode, 2000);
container.notifyObservers();
if(callback!=null){
callback.callBackMethod();
}
};
mTimerTaskHelper = TimerTaskHelper.getInstance(container);
mTimerTaskHelper.setCallBack(new CallBack(){
void callBackMethod(){
//TODO your code
}});
我想你想要这样的东西
interface CallBack {
void callBackMethod();
}
public class TimerTaskHelper {
private static TimerTaskHelper instance = null;
private static Integer ONE_MINUTE = 60000;
private static Handler handler;
private static Runnable runnableInstance;
private static Container container;
private CallBack callback;
public void setCallBack(CallBack cb){
callback=cb;
}
public static TimerTaskHelper getInstance(Container c) {
if (instance == null) {
instance = new TimerTaskHelper(c);
}
return instance;
}
public TimerTaskHelper(Container c) {
this.handler = new Handler();
this.runnableInstance = runnableCode;
this.container = c;
// Kick off the first runnableInstance task right away
handler.post(runnableCode);
}
// Define the task to be run here
private Runnable runnableCode = new Runnable() {
@Override
public void run() {
// Do something here on the main thread
Logger.d("Called");
// Repeat this runnableInstance code again every 2 seconds
handler.postDelayed(runnableCode, 2000);
container.notifyObservers();
if(callback!=null){
callback.callBackMethod();
}
};
mTimerTaskHelper = TimerTaskHelper.getInstance(container);
mTimerTaskHelper.setCallBack(new CallBack(){
void callBackMethod(){
//TODO your code
}});
我想你想要这样的东西
interface CallBack {
void callBackMethod();
}
public class TimerTaskHelper {
private static TimerTaskHelper instance = null;
private static Integer ONE_MINUTE = 60000;
private static Handler handler;
private static Runnable runnableInstance;
private static Container container;
private CallBack callback;
public void setCallBack(CallBack cb){
callback=cb;
}
public static TimerTaskHelper getInstance(Container c) {
if (instance == null) {
instance = new TimerTaskHelper(c);
}
return instance;
}
public TimerTaskHelper(Container c) {
this.handler = new Handler();
this.runnableInstance = runnableCode;
this.container = c;
// Kick off the first runnableInstance task right away
handler.post(runnableCode);
}
// Define the task to be run here
private Runnable runnableCode = new Runnable() {
@Override
public void run() {
// Do something here on the main thread
Logger.d("Called");
// Repeat this runnableInstance code again every 2 seconds
handler.postDelayed(runnableCode, 2000);
container.notifyObservers();
if(callback!=null){
callback.callBackMethod();
}
};
mTimerTaskHelper = TimerTaskHelper.getInstance(container);
mTimerTaskHelper.setCallBack(new CallBack(){
void callBackMethod(){
//TODO your code
}});
第一件事:让你的属性。。。静止的第一件事:让你的属性。。。静止的第一件事:让你的属性。。。静止的第一件事:让你的属性。。。静止的