简体   繁体   English

在Android中访问全局变量

[英]Accessing global Variables in Android

I have a class file which extends SurfaceView and implements SurfaceHolder.Callback which is saved as myView.java 我有一个扩展SurfaceView并实现SurfaceHolder.Callback的类文件,该文件另存为myView.java

public class myView extends SurfaceView implements SurfaceHolder.Callback

Now i got another java file GlobalVar.java which is like this 现在我得到了另一个Java文件GlobalVar.java,就像这样

import android.app.Application;

class GlobalVar extends Application {

private int touchCount;

public int getCount()
  {
  return touchCount;
  }

public void setCount(int tCount)
  {
  touchCount = tCount;
  }
}

Inside myView class(myView.java) there is a function onTouchEvent 在myView类(myView.java)中,有一个onTouchEvent函数

@Override
public boolean onTouchEvent(MotionEvent event) {
   int pointerCount = event.getPointerCount();
   }

Now what i want is to set the the global Variable touchCount to pointCount. 现在我想要将全局变量touchCount设置为pointCount。 And i tried like this 我尝试过这样

@Override
public boolean onTouchEvent(MotionEvent event) {
    int pointerCount = event.getPointerCount();
    GlobalVar tcount = ((GlobalVar)getApplicationWindowToken());
tcount.setCount(pointerCount);
  } 

And this is giving me error.So how can i set the global variable value inside this myView class file??? 这给了我错误。那么如何在此myView类文件中设置全局变量值呢?

Application portion of manifest file 清单文件的应用程序部分

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".MyViewActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>
<application android:name=".GlobalVar"  android:icon="@drawable/icon"                 android:label="@string/app_name">
</application>

May be you'll need a some singleton class which will hold all you variables? 可能需要一个单例类来容纳所有变量?

public class GlobalVar {

    public int getMyVar() {
        return myVar;
    }

    public void setMyVar(int myVar) {
        this.myVar = myVar;
    }

    private int myVar = 0;
    private static GlobalVar instance;

    static {
        instance = new GlobalVar();
    }

    private GlobalVar() {
    }

    public static GlobalVar getInstance() {
        return GlobalVar.instance;
    }

}

Usage pattern: 使用方式:

@Override
public boolean onTouchEvent(MotionEvent event) {
    int pointerCount = event.getPointerCount();
    GlobalVar.getInstance().setMyVar(pointerCount);
} 

I would recommend using SharedPreferences . 我建议使用SharedPreferences

Example: 例:

// Have a SharedPreferences class 

SharedPreferences pref = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
   public final static String WIFI_NAME = "WIFI_NAME";
   public void setWifiName(String wifi_name) {
   Editor edit = pref.edit();
   edit.putString(WIFI_NAME, wifi_name);
   edit.commit();
}

public String getWifiName() {
   return pref.getString(WIFI_NAME, "");
}

Then, to access it: 然后,访问它:

string name = pref.getWifiName();  // GET VALUE
pref.setWifiName(name ); //SET VALUE

Try this instead: 尝试以下方法:

    @Override
public boolean onTouchEvent(MotionEvent event) {
   int pointerCount = event.getPointerCount();
   GlobalVar gv = (GlobalVar) getContext.getApplicationContext();
   gv.setCount(pointerCount);
   }

What you are doing fails because the object returned by that method isn't the Application object. 您正在执行的操作失败,因为该方法返回的对象不是Application对象。 I should also mention that you need to register the name of your custom Application class in the manifest using android:name="GlobalVar" tag in your Application node. 我还应该提到,您需要在清单中使用Application节点中的android:name =“ GlobalVar”标签注册自定义Application类的名称。

Change: 更改:

getApplicationWindowToken()

to: 至:

getApplicationContext()

Shouldn't you set the android:name attribute on the application that contains your Activity ? 您是否应该在包含Activityapplication上设置android:name属性? So I would think your manifest should be: 因此,我认为您的清单应为:

<application android:name=".GlobalVar" android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".MyViewActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

I've not tried this myself but that's the first thing I'd try. 我自己还没有尝试过,但这是我要尝试的第一件事。

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

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