SORU
4 Ocak 2010, PAZARTESİ


Android Bağlam almak için statik bir Yol?

Bir şekilde statik bir yöntem kullanarak, mevcut Bağlamda örnek almak için var mı?

Bağlam örneğini değiştirir her zaman tasarrufu nefret ediyorum çünkü o yolu arıyorum.

CEVAP
25 ŞUBAT 2011, Cuma


Bunu yapmak

Android Manifest dosyası aşağıdaki bildirin

<application android:name="com.xyz.MyApplication">

</application>

sonra sınıf yazmak

public class MyApplication extends Application{

    private static Context context;

    public void onCreate(){
        super.onCreate();
        MyApplication.context = getApplicationContext();
    }

    public static Context getAppContext() {
        return MyApplication.context;
    }
}

MyApplication.getAppContext() Uygulama içeriği almak için çağrı nerede şimdi her statik olarak.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Elliot Davin

    Elliot Davin

    28 Kasım 2008
  • GoogleTechTalks

    GoogleTechTa

    15 AĞUSTOS 2007
  • The White House

    The White Ho

    21 Ocak 2006