Java-将静态方法数据动态传递给方法

时间:2018-07-25 16:03:26

标签: java android static-methods

因此,我试图在我的应用程序中不断更新Box的位置,但是我检索数据的方式是从静态方法获取的。 我尝试执行以下操作,但是没有运气。 希望有人可以引导我正确地执行此操作。

Public class Foo extends service{
private ImageView box;
    static int dataX;
    static int dataY;

public void onCreate() {

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    box = new ImageView(this);
    box.setImageResource(R.drawable.imageX);


final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

                params.x = 0
                params.y = 0
        windowManager.addView(box, params);

           // The Box position should be changing constantly from the data I retrieve 

               box.setX(dataX);
               box..setY(dataY);
            windowManager.updateViewLayout(box, params);



..
..

// and the code above is not working, since im not passing the data correctly


}

public static void Data(int x, int y) {
// the data is being passed to this method
// x and y contains dynamic data which changes constantly
 dataX = x;
 dataY = y;

}

}

2 个答案:

答案 0 :(得分:0)

Public class Foo extends service{
private ImageView box;
    static int dataX;
    static int dataY;

public void onCreate() {

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    box = new ImageView(this);
    box.setImageResource(R.drawable.imageX);


    //Did you call the method like this?
      Data(dataX, dataY);
    //

final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

                params.x = 0
                params.y = 0
        windowManager.addView(box, params);

           // The Box position should be changing constantly from the data I retrieve 

               box.setX(dataX);
               box..setY(dataY);
            windowManager.updateViewLayout(box, params);



..
..

// and the code above is not working, since im not passing the data correctly


}

public static void Data(int x, int y) {
// the data is being passed to this method
// x and y contains dynamic data which changes constantly
 dataX = x;
 dataY = y;

}

}

答案 1 :(得分:0)

重写:

如果这是针对rmi的,请使用静态方法将rmi接口拆分为它自己的类。应该很简单。

所有工作都可以在另一个非静态(普通)类中完成,而您的静态rmi类可以保留对该实例的引用。