简体   繁体   English

如何在不同的类中引用相同的类变量?

[英]How can I reference the same class variable in different classes?

I have a variable "wl" of type "PowerManager.WakeLock". 我有一个类型为“ PowerManager.WakeLock”的变量“ wl”。 I need to use the same "wl" variable and not a different object in different classes. 我需要在不同的类中使用相同的“ wl”变量,而不是不同的对象。 Can this be done? 能做到吗?

 public PowerManager.WakeLock wl; 
 // This variable needs to be used like a switch in other classes. 
 // In one class its turned on, in another class I need to call 
 // a method .Release(), to turn it off, 
 // but it has to refer to that same "wl" variable.

NotificationReceiver CLass - "holds the power manager NotificationReceiver CLass-“持有电源管理器

     public class NotificationReceiver extends Service {
public PowerManager powerManager = (PowerManager) 
      getSystemService(POWER_SERVICE);
public static PowerManager.WakeLock wl = 
       powerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, 
       "My:WakelockTag"); //Non-static field 'powerManager' cannot be referenced from a static context


@Override
public void onCreate() {
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {


    wl.acquire();


    return START_NOT_STICKY;

}

  //--------------------------------------------------------------------

MainActivity class that I need to use the that variable in 我需要在其中使用that变量的MainActivity类

public class MainActivity extends AppCompatActivity {
       @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Button button = (Button) findViewById(R.id.start_button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {


            Intent serviceIntent = new Intent (MainActivity.this, ExampleService.class);

            startService(serviceIntent);





            NotificationReceiver nn = new NotificationReceiver();// Here I make a new object to access the .aquire() method 

            nn.wl.acquire();

        }
    });

您可以在构造类时或使用它的各个函数中传递它。

It can be done by making your variable static. 可以通过将变量设为静态来完成。 You can access it by ClassName.variable. 您可以通过ClassName.variable访问它。 (It will be field of only one class, but it can be accessed from all your classes) (这将是一个班级的字段,但是可以从您所有班级中访问它)

Consider Singleton pattern: 考虑Singleton模式:

class LockHolder {

  public static final LockHolder INSTANCE = new LockHolder();

  private final PowerManager.WakeLock wakeLock;

  // constructor is private so that noone can create new instances
  private LockHolder() { 
    this.wakeLock = new WakeLock();
  }

  // accessor method is safer than exposing the variable itself;
  // you may eventually end up doing more things here
  public PowerManager.WakeLock wakeLock() {
    return wakeLock;
  }
}

LockHolder is a Singleton since only one instance of this class exists. LockHolderSingleton因为仅存在此类的一个实例。

You use it as follows: 您可以如下使用它:

PowerManager.WakeLock lock = LockHolder.INSTANCE.wakeLock();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM