Java 如何将活动上下文放入非活动类中?
我有一个活动类,从中我将一些信息传递给助手类(非活动)类。在helper类中,我想使用Java 如何将活动上下文放入非活动类中?,java,android,android-context,Java,Android,Android Context,我有一个活动类,从中我将一些信息传递给助手类(非活动)类。在helper类中,我想使用getSharedReferences()。但我无法使用它,因为它需要活动上下文 这是我的密码: class myActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-genera
getSharedReferences()
。但我无法使用它,因为它需要活动上下文
这是我的密码:
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);
}
}
知道如何实现吗?您需要将上下文传递给非活动类的构造函数
ItemsStore itemstore = new ItemStore(myActivity.this);
itemstore.SetItems(Info);
然后
现在,mContext
可以用作活动上下文
注意:不要保留对上下文活动的长期引用(对活动的引用应与活动本身具有相同的生命周期)尝试以下操作:
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);
}
}
在活动中编写一个公共函数。在活动类中创建助手类的实例时,在构造函数中传递活动的上下文
然后从助手类中,使用活动上下文调用活动类中的公共函数。您可以尝试此解决方案,而不是创建内存泄漏(通过在类字段中保存活动上下文),因为共享首选项不需要活动上下文,而是。。。任何上下文:)对于长寿对象,应使用ApplicationContext 创建应用程序类: 在舱单上登记 方法签名这样看起来更好,因为它不需要外部上下文。这可以隐藏在某些界面下。您还可以轻松地将其用于依赖项注入
HTH将
context
传递给ItemsStoremake上下文的构造函数,然后在非活动类中传递此上下文对象。
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);
}
}
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);
}