简体   繁体   English

非活动扩展类上的SharedPreferences

[英]SharedPreferences on a non-Activity extended class

I have a class that sends some info to the mysql db. 我有一个类,将一些信息发送到mysql数据库。 I want from this class to use the Shared Preferences methods but they are belong to the activity class. 我希望从此类使用“共享首选项”方法,但它们属于活动类。 My class extends AsyncTask so I can't extend another.. I tried to create an activity instance and use it but my program has stoped. 我的班级扩展了AsyncTask,所以我不能扩展其他类。我试图创建一个活动实例并使用它,但我的程序已停止。 something like this: 像这样的东西:

Activity a1 = new Activity();
SharedPreferenecs loginInfo = a1.getSharedPreferences("MyKid", 0); 

etc .. 等..

pass the Context of your Activity in non Activity class's Constructor. 在非Activity类的构造方法中传递Activity的上下文。

and then use 然后使用

mContext.getSharedPreferences("MyKid", 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM