get Context in non-Activity class
If your class is non-activity class, and creating an instance of it from the activiy, you can pass an instance of context via constructor of the later as follows:
class YourNonActivityClass{
// variable to hold context
private Context context;
//save the context recievied via constructor in a local variable
public YourNonActivityClass(Context context){
this.context=context;
}
}
You can create instance of this class from the activity as follows:
new YourNonActivityClass(this);
How to get activity context into a non-activity class android?
Try this:
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);
}
}
Get Context in non-activity class multiple times
Change
this.cntxt = context;
To
cntxt = context.getApplicationContext();
instead. Where cntxt
is a static context. It will not create activity leak, since it uses application context.
It is better if you learn about background service in Android
https://developer.android.com/training/run-background-service/create-service.html
Context inside non activity class
public Example(Context context){
this.context = context.getApplicationContext();
}
why? use
this.context = context;
instead.
or
wifihist = new wifiHistoryDatabaseHandler(getActivity());
The context variable in telnet class is null.
You can consider to pass context directly in the constructor of Telnet async task
public Telnet(Context c)
{
mContext = c;
}
How to retrieve a context from a non-activity class?
If class B requires a Context to operate, then I don't see any problem having class A provide that to it (through a parameter on the play method, a parameter in a constructor, etc).
I don't think you are doing any poor OOP by providing class B the dependencies that it needs to do it's job.
Related Topics
How to Add Action Bar from Support Library into Preferenceactivity
Android Split Action Bar with Action Items on the Top and Bottom
Android Textview:"Do Not Concatenate Text Displayed with Settext"
Android Asynctask Testing with Android Test Framework
Wrong Requestcode in Onactivityresult
How to Keep an Alertdialog Open After Button Onclick Is Fired
How to Implement the Material-Design Elevation for Pre-Lollipop
Cannot Resolve Symbol 'Theme' in Styles.Xml (Android Studio)
Accessing UI Thread Handler from a Service
Detecting Which Selected Item (In a Listview) Spawned the Contextmenu (Android)
Android: How to Create an "Ongoing" Notification
Get Thumbnail Uri/Path of the Image Stored in Sd Card + Android
Get Focused View from Viewpager