如何在Android的非Activity类中获取Activity上下文?

15

我有一个Activity类,从这里我要向一个帮助类(非Activity类)传递一些信息。在帮助类中,我想使用getSharedPreferences()方法。但是我无法使用它,因为它需要Activity上下文。

以下是我的代码:

  class myActivity extends Activity
    {
    @Override
        protected void onCreate(Bundle savedInstanceState) 
        {

            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.home);


            Info = new Authenticate().execute(ContentString).get();
            ItemsStore.SetItems(Info);

        }

    }

class ItemsStore
{
  public void SetItems(Information info)
 {
  SharedPreferences  localSettings = mContext.getSharedPreferences("FileName", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = localSettings.edit();
            editor.putString("Url", info.Url);
            editor.putString("Email", info.Email);
 }
}

有任何想法可以实现这个吗?


2
context传递给ItemsStore的构造函数。 - Shayan Pourvatan
在您的非 Activity 类中创建 Context,然后将此 Context 对象传递到需要它的地方。 - Amarnath Baitha
4个回答

28

不要在类字段中持有活动上下文以创建内存泄漏, 您可以尝试此解决方案,因为共享首选项不需要活动上下文,而是任何上下文:) 对于长期存在的对象,您应该使用ApplicationContext。

创建应用程序类:

public class MySuperAppApplication extends Application {
    private static Application instance;

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

    public static Context getContext() {
        return instance.getApplicationContext();
    }
}

在清单文件中注册它

<application
    ...
    android:name=".MySuperAppApplication" >
    ...
</application>

那么你可以像这样做

public void persistItems(Information info) {
    Context context = MySuperAppApplication.getContext();
    SharedPreferences sharedPreferences = context.getSharedPreferences("urlPersistencePreferences", Context.MODE_PRIVATE);
    sharedPreferences.edit()
        .putString("Url", info.Url)
        .putString("Email", info.Email);
}

这种方法签名看起来更好,因为它不需要外部上下文。这可以隐藏在一些接口下。你也可以轻松地将其用于依赖注入。

希望对你有所帮助。


10

试试这个:

class myActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {

        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);


        Info = new Authenticate().execute(ContentString).get();
        ItemsStore.SetItems(Info, getApplicationContext());

    }

}

class ItemsStore
{
   public void SetItems(Information info, Context mContext)
   {
            SharedPreferences  localSettings = mContext.getSharedPreferences("FileName",
            Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = localSettings.edit();
            editor.putString("Url", info.Url);
            editor.putString("Email", info.Email);
   }
}

8

您需要将上下文传递给非活动类的构造函数

ItemsStore itemstore = new ItemStore(myActivity.this);
itemstore.SetItems(Info);

然后。
Context mContext;
public ItemsStore (Context context)
{
       mContext =context;
}

现在mContext可以用作Activity上下文。
注意:不要保留对上下文活动的长期引用(对活动的引用应与活动本身具有相同的生命周期)。

0
在您的活动中编写一个公共函数。在Activity类中创建您帮助类的实例时,在构造函数中传递活动的上下文。
然后在您的帮助类中,使用活动上下文调用活动类中的公共函数。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接