android-context

in what case does bindService return false?

我与影子孤独终老i 提交于 2019-11-29 09:38:53
I was wondering when does Context.bindService() return false ? I've tried to make the onBind() return null , but it still returns true when bindService is called and the onServiceConnected does not get executed. I also saw this on Google Groups with no response https://groups.google.com/forum/#!topic/android-developers/ZLl56Mz1jYg I also can't find the implementation of bindService, since it is abstract in Context.java and searching for "public boolean bindService" does not yield any useful results either (closest was ApplicationContext which does not seem to be present in the current API

Passing a activity context into a static method, memory leak potential?

笑着哭i 提交于 2019-11-29 06:27:36
I've seen this particular technique for launching activities and it seems to me like a bad idea because of static contexts but I was hoping someone might have a legit reason behind this approach. The activity you want to launch implements a static launch(Context context) method that sets up the intent, flags, etc and finally starts the activity. public static void launch(Context context){ Intent i = new Intent(context, SomeOtherActivity.class); // flag stuff context.startActivity(i); } Then a DifferentActivity could go and launch SomeOtherActivity with one line. SomeOtherActivity.launch

Calling context.getResources() returns null

夙愿已清 提交于 2019-11-29 06:14:28
So I am trying to get a string resource in my project but when I called context.getResources().getString(...) , I get a NullPointerException . In debug mode, I found out that the context isn't null but looking at its members, I found out that mResources was null. Why are the resources not loaded for the activity context? EDIT Apparently, this is what triggered the Exception public class MyActivity extends Activity { SomeClass someClass = new SomeClass(this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } } public class SomeClass { private

Android JUnit4 Testing - Where to get Context from?

安稳与你 提交于 2019-11-29 05:29:41
问题 I have to build an app with sqlite usage. Now I want to write my unit tests. These unit tests should test my class SQLiteBridge . SQLiteBridge provides DAOs for every child class of Model. Now I got the problem that I need a context to create my SQLiteBridge . SQLiteBridge creates and handles a SQLite database on the system.. Where to get the Context-Object from? My setup is like here (so I'm using Junit4 [thanks god]): http://tools.android.com/tech-docs/unit-testing-support EDIT: I hope

Android context leaks in AsyncTask

与世无争的帅哥 提交于 2019-11-29 03:56:06
If I interpret this article correctly, passing the activity context to AsyncTasks is a potential leak, as the activity might be destroyed while the task is still running. How do you deal with this in AsyncTasks that are not inner clases and need access to resources or to update the UI? Additionally, how can you avoid leaking the context if you need references to progress dialogs to dismiss them? If I understand your question correctly: Java's WeakReference or SoftReference class is a good fit for this type of situation. It will allow you to pass the context to the AsyncTask without preventing

Finish the calling activity when AsyncTask completes

岁酱吖の 提交于 2019-11-29 03:44:47
My calling activity: public class Hello extends Activity { public void onCreate(Bundle savedInstanceState) { MyTask mt = new MyTask(this); mt.execute(); } Now In MyTask (an external class): public class MyTask extends AsyncTask<Void, Void, Void> { private Context mContext; public MyTask(Context context) { mContext = context; } //doinbackground, etc protected void onPostExecute() { mContext.finish(); } Other things are working as expected if I remove mContext.finish() above. But if I'm calling mContext.finish() , I'm getting an error: The method finish() is undefined for the type Context

AsyncTask and Contexts

偶尔善良 提交于 2019-11-29 00:53:49
问题 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 exactly sure what the Context is or how to use it on background threads, and I haven't found any good examples on it. Any help? Here is an excerpt of what I'm

Retrieve Context from a fragment

倖福魔咒の 提交于 2019-11-28 21:22:24
I created a class to retrieve comments from a JSON encoding from a PHP file. This class, extends from AsyncTask: public class RecuperarComentarisFoto extends AsyncTask<String, String, String>{ @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(????); pDialog.setMessage("Creating Product.."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } @Override protected String doInBackground(String... arg0) { // TODO Auto-generated method stub return null; } @Override protected void onPostExecute(String result) { // TODO Auto

Needing Context in non-Activity classes

独自空忆成欢 提交于 2019-11-28 21:02:21
I have some classes within my application that need to call Android functions that require the Context as a parameter. I don't have it as the class is not a subclass of the Activity class. What is the correct way to tackle this problem? Pass it as a parameter on each call? Pass it at class instantiation and keep it? Maxim It depends on the role of the class. But anyway pass ApplicationContext but not Activity one. If you pass Activity context gc can't remove it from the memory when after you don't need activity anymore. But application context is used while application was not finished by OS

Bluetooth device discovery in Android — startDiscovery()

守給你的承諾、 提交于 2019-11-28 20:34:45
问题 Goal: Build an Android app that discovers the names and addresses of BT devices within range and submits their values to a webservice. BT devices have not been previously bonded to the host device, I just want to poll everything as I walk about. What I've done: Pored over documentation. Implemented a local instance of the host device's BT adapter. Implemented a notification to enable BT if it isn't enabled. Registered Broadcast Receivers and Intents to parse the ACTION_FOUNDs coming off of