java - 如何将一个类的 "Context"使用到另一个类中 - Android

标签 java android class android-context

我在一个类中使用上下文。我如何在另一个类中启动它时引用相同的上下文?

MyHttpClient.java

public class MyHttpClient extends DefaultHttpClient {


    final Context contextkey;

    public MyHttpClient(Context contextkeystore) {
        this.contextkey = contextkeystore;
    }

    @Override
    protected ClientConnectionManager createClientConnectionManager() {
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        // Register for port 443 our SSLSocketFactory with our keystore
        // to the ConnectionManager
        registry.register(new Scheme("https", newSslSocketFactory(), 443));
        return new SingleClientConnManager(getParams(), registry);
    }

    private SSLSocketFactory newSslSocketFactory() {
        try {
            // Get an instance of the Bouncy Castle KeyStore format
            KeyStore trusted = KeyStore.getInstance("BKS");
            // Get the raw resource, which contains the keystore with
            // your trusted certificates (root and any intermediate certs)
            InputStream in = contextkey.getResources().openRawResource(R.raw.mykeystore);
            try {
                // Initialize the keystore with the provided trusted certificates
                // Also provide the password of the keystore
                trusted.load(in, "mysecret".toCharArray());
            } finally {
                in.close();
            }
            // Pass the keystore to the SSLSocketFactory. The factory is responsible
            // for the verification of the server certificate.
            SSLSocketFactory sf = new SSLSocketFactory(trusted);
            // Hostname verification from certificate
            // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506
            sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
            return sf;
        } catch (Exception e) {
            throw new AssertionError(e);
        }
    }
}

在另一个类中使用:

DefaultHttpClient client = new MyHttpClient(getApplicationContext()); ?? 

这里“getApplicationContext”不起作用并显示错误

最佳答案

获取应用程序上下文的简单方法如下

package com.example.testactivity;

        public class App extends Application {
        public static Context context;

    public void onCreate() {
                    super.onCreate();
                    context=getApplicationContext();
                           }
    public static Context getcontext(){
     return context;
     }

   }

并在 list 文件中 在应用程序标记中添加该行

<application
        android:name="com.example.testactivity.App" 
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

现在您将在应用程序中的任何位置查看应用程序的上下文

只需调用

App.getcontext();

很高兴:)

关于java - 如何将一个类的 "Context"使用到另一个类中 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24760457/

相关文章:

java - 如何更改首选项 Activity 的背景?

c# - 返回类的实例时,是否返回所有属性?

c++ - 如何选择 C++ 中的运算符重载?

java - 从另一个 Activity Android studio 导入数据

java - 如何在类里面成功回调时调用 toast 消息

java - Hssf Bold 属性在 Excel 2007-10 的部分文件中不起作用

java - KieScanner 与远程 Maven 存储库

android - 在 Android 中解析 XML 文件时遇到 StringIndexOutOfBoundsException

ruby - 我的类名与 Ruby 的冲突

java - 如何防止异步线程中的对象被修改?