android-context

Using Application context everywhere?

In an Android app, is there anything wrong with the following approach: public class MyApp extends android.app.Application { private static MyApp instance; public MyApp() { instance = this; } public static Context getContext() { return instance; } } and pass it everywhere (e.g. SQLiteOpenHelper) where...

Android - what's the difference between the various methods to get a Context?

In various bits of Android code I've seen: public class MyActivity extends Activity { public void method() { mContext = this; // since Activity extends Context mContext = getApplicationContext(); mContext = getBaseContext(); } } However I can't find any decent explanation of which is preferable, and u...

Android service not starting via alarm

Is there any problem with launching a service via an Alarm's BroadcastReceiver's onReceive? My service never has it's onCreate or onStart method called. My Alarm's receiver gets a wakelock, calls context.startService(new Intent(context, FmiDaemon.class)); (context given via onRecieve parameter), waits 3 seconds, then releases the lock....

Add Context Menu Icon in android

I have a Listview with a ContextMenu, but when I setIcon for ContextMenu look like it doesn't work public void onCreateContextMenu(ContextMenu menu , View v, ContextMenuInfo menuInfo){ super.onCreateContextMenu(menu, v, menuInfo); menu.add(0, DELETE_ID, 0, R.string.context_menu_favorite) .setIcon(android.R.drawable.b...

Getting Android's system preferences without having any context

In an Android utility class, I want to get a system preference value in a class, but I don't have the context there, because the class that calls it doesn't have the context either. I've found that for Resources one can use the static Resources.getSystem() function. Is there any similar way for getting system preferences without context?...

AsyncTask and Contexts

So I'm working out my first multi-threaded application using Android with the AsyncTask class. I'm trying to use it to fire off a Geocoder in a second thread, then update the UI with onPostExecute, but I keep running into an issue with the proper Context. I kind of hobbled my way through using Contexts on the main thread, but I'm not ex...

Android AsyncTask context problem

I've been working with AsyncTasks in Android and I am dealing with a strange issue. Take a simple example, an Activity with one AsyncTask. The task on the background does not do anything spectacular, it just sleeps for 8 seconds. At the end of the AsyncTask in the onPostExecute() method I am just setting a button visibility status to V...

Android - setResultData(null) doesn't properly block call on hero?

Following on from my Question Here i have found what the problem is but cant seem to fix it. What appears to be happening is that my Application launches, the user can then press a button to start a background service and the Activity disappears. The user then makes a call and from here my applications dialog screen appears. In the int...

Prevent Context Menu from closing on clicking item (specifically checkbox item)

If I have a checkable item in a Context Menu or ordianry Menu, how do I prevent the menu from closing when the item is selected? ...

Can a home widget have a Context?

In an activity it's (usually) easy to get the Context. What if I am working with a home widget class? These are classes that extends AppWidgetProvider, which don't contain a Context! Thank you. ...

Which Context to use in a activity class?

I have a class defined as public class viewGroups extends ListActivity Somewhere in the class I have objItem = new clsContactGroups(context); I am wondering what is advised to be used here? Which context? I know four choices, but maybe there are others... this this.getApplicationContext() this.getBaseContext() this.getParent() ...

Best way to get an Application Context into a static method in Android

I'm working on an Android application that has several Activities. In it I have a class with several static methods. I would like to be able to call these methods from the different Activities. I'm using the static methods to load data from an xml file via a XmlResourceParser. To create a XmlResourceParser requires a call on the Applicat...

Android : References to a Context and memory leaks

I've read that it is a mistake and a source of memory leaks in Android application to keep a long-lived references to a Context. But I don't understand if it is ok to create an class that looks like this one: public class HelperClass { private Context context; public HelperClass(Context context) { this.context = contex...

Android - Getting Exception

I am developing one application in which i am getting an exception, and i know this one is the silly or small mistake which i am doing but your help may catch me out and make my day: public class Demo extends Activity { Button btnDemo; Thread t; AlertDialog alertDialog; @Override public void onCreate(Bundle...

How to access context in non activity classes

Hi, I am writing my first Android application and have some problems with handing around context objects. A lot of methods/constructors seem to require the current context (activity) which causes me some problems in my action listeners. Instead of having all my listeners and handlers as anonymous classes in the activity class I have c...