2010-12-08 62 views

回答

310
  1. 创建的Application一个子类,例如public class App extends Application {
  2. 坐落在AndroidManifest.xml<application>标签的android:name属性指向你的新类,例如android:name=".App"
  3. 在您的应用实例的onCreate()方法中,将您的上下文(例如this)保存到名为mContext的静态字段中,并创建一个返回此字段的静态方法。 getContext()

这是它应该如何看:

public class App extends Application{ 

    private static Context mContext; 

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

    public static Context getContext(){ 
     return mContext; 
    } 
} 

现在你可以使用:App.getContext()每当你想获得一个上下文,然后getResources()(或App.getContext().getResources())。

+0

整洁的伎俩,谢谢。好吧,那么没有标准的API呢? – 2010-12-09 18:03:09

+1

这是标准的,这不是一个伎俩XD – Cristian 2010-12-09 18:13:05

+0

我同意这是一个丑陋的黑客。永远不要为动态值赋予动态值 – Bostone 2012-02-22 23:46:39

92

使用

Resources.getSystem().getString(android.R.string.cancel) 

你可以在你的应用程序在任何地方使用它们,即使是在静态常量声明! 但仅限于系统资源!

0

我想,更多的方式是可行的。 但有时候,我使用这个解决方案。 (全全球):

import android.content.Context; 

    import <your package>.R; 

    public class XmlVar { 

     private XmlVar() { 
     } 

     private static String _write_success; 

     public static String write_success() { 
      return _write_success; 
     } 


     public static void Init(Context c) { 
      _write_success = c.getResources().getString(R.string.write_success); 
     } 
    } 
//After activity created: 
cont = this.getApplicationContext(); 
XmlVar.Init(cont); 
//And use everywhere 
XmlVar.write_success(); 
0

在你的类,在这里实现静态功能,可以调用一个私人\公共从这个类的方法。私人\ public方法可以访问getResources

例如:

public class Text { 

    public static void setColor(EditText et) { 
     et.resetColor(); // it works 

     // ERROR 
     et.setTextColor(getResources().getColor(R.color.Black)); // ERROR 
    } 

    // set the color to be black when reset 
    private void resetColor() { 
     setTextColor(getResources().getColor(R.color.Black)); 
    } 
} 

,并从其他类\活动,您可以拨打:

Text.setColor('some EditText you initialized'); 
0

,如果你有一个背景下,我的意思是里面;

public void onReceive(Context context, Intent intent){ 

}

您可以使用此代码来获得资源:

context.getResources().getString(R.string.app_name); 
3

的辛格尔顿:

package com.domain.packagename; 

import android.content.Context; 

/** 
* Created by Versa on 10.09.15. 
*/ 
public class ApplicationContextSingleton { 
    private static PrefsContextSingleton mInstance; 
    private Context context; 

    public static ApplicationContextSingleton getInstance() { 
     if (mInstance == null) mInstance = getSync(); 
     return mInstance; 
    } 

    private static synchronized ApplicationContextSingleton getSync() { 
     if (mInstance == null) mInstance = new PrefsContextSingleton(); 
     return mInstance; 
    } 

    public void initialize(Context context) { 
     this.context = context; 
    } 

    public Context getApplicationContext() { 
     return context; 
    } 

} 

初始化辛格尔顿在Application子类:

package com.domain.packagename; 

import android.app.Application; 

/** 
* Created by Versa on 25.08.15. 
*/ 
public class mApplication extends Application { 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     ApplicationContextSingleton.getInstance().initialize(this); 
    } 
} 

如果我没有错,这会给你一个hook到applicationContext的地方,用ApplicationContextSingleton.getInstance.getApplicationContext(); 来调用它。你不需要在任何时候清除它,因为当应用程序关闭的时候,它总是与它一起使用。

记住更新AndroidManifest.xml使用此Application子类:

<?xml version="1.0" encoding="utf-8"?> 

<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.domain.packagename" 
    > 

<application 
    android:allowBackup="true" 
    android:name=".mApplication" <!-- This is the important line --> 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" 
    android:icon="@drawable/app_icon" 
    > 

现在,你应该能够使用ApplicationContextSingleton.getInstance()getApplicationContext()从任何地方getResources(),还有极少数的地方。应用程序子类不能。

如果您在这里看到任何错误,请让我知道,谢谢。 :)

1

另一种解决方案:

如果在非静态外部类的静态子类,你可以从子类中通过在外部类的静态变量,就是你在创作的初始化访问的资源外部类。像

public class Outerclass { 

    static String resource1 

    public onCreate() { 
     resource1 = getString(R.string.text); 
    } 

    public static class Innerclass { 

     public StringGetter (int num) { 
      return resource1; 
     } 
    } 
} 

我用了我的FragmentActivity内的静FragmentPagerAdapter因为I8N的,这是有用的getPageTitle(INT位置)功能。

1

还有另一种可能性。我从加载资源的OpenGL着色器是这样的:

static private String vertexShaderCode; 
static private String fragmentShaderCode; 

static { 
    vertexShaderCode = readResourceAsString("/res/raw/vertex_shader.glsl"); 
    fragmentShaderCode = readResourceAsString("/res/raw/fragment_shader.glsl"); 
} 

private static String readResourceAsString(String path) { 
    Exception innerException; 
    Class<? extends FloorPlanRenderer> aClass = FloorPlanRenderer.class; 
    InputStream inputStream = aClass.getResourceAsStream(path); 

    byte[] bytes; 
    try { 
     bytes = new byte[inputStream.available()]; 
     inputStream.read(bytes); 
     return new String(bytes); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     innerException = e; 
    } 
    throw new RuntimeException("Cannot load shader code from resources.", innerException); 
} 

正如你所看到的,你可以在路径/res/... 变化aClass访问任何资源类。这也是我如何加载测试资源(androidtest)