GetSystemService DOWNLOAD_SERVICE in non activity - android

In my simple method in non activity class, I am using code:
mgr=(DownloadManager)mContext.getSystemService(DOWNLOAD_SERVICE);
in non activity class, my constructor looks like:
public Download23(Context context){
this.mContext=context;
}
But compilator won't accept DOWNLOAD_SERVICE string. Do you know how to solve that?

Instead you could just write
(DownloadManager)mContext.getSystemService(Context.DOWNLOAD_SERVICE);
That would compile.
DOWNLOAD_SERVICE is a constant of Context class.

you can use
(DownloadManager)mContext.getSystemService(Context.DOWNLOAD_SERVICE);

Related

How to use UI functions in non UI methods?

I want create a own class with utility functions.
Therefore i want to use typical Activity-functions and build something around them.
I want use the setTitle function for example:
public void frameworkSetTitle() {
String testValue;
setTitle(testValue);
}
Is it possible to use this functions in own classes and pass the result back to the calling Activity?
You have to pass the Activity instance to the util method and call the desired methods there, like setTtitle.
This functionality is not recommended, as passing around context can lead to unintended memory leaks. Flow of information should be uni-directional, anyway.
Your util class should do something more like this:
public static String getGeneratedTitle() {
String titleValue;
// some work
return titleValue;
}
This way, you can just make a simple call in your activity:
myTextView.setText(myUtilClass.getGeneratedTitle());

Android DataBinding where to get context?

I have TextView for showing time. I want to use Android's DataBinding plugin.
For formatting time I am using DateUtils.formatDateTime(context, int, int) method which takes Context instance. Is it possible to get context include element? Or do I have to use old school way?
Thanks
Also you can do something like this in your view using the current view context as parameter.
...
android:text="#{yourModelHere.yourModelMethodHere(context)}"
...
Thought I should answer instead of putting in a comment. You'll have more options when rc2 is released. In rc1, you can pass the context in a variable to the Binding, then pass it as a parameter to the method. Alternatively, you can create a custom attribute for data binding:
#BindingAdapter({"timeMillis", "dateFlags"})
public static void setDateText(TextView view, int timeMillis, int dateFlags) {
view.setText(DateUtils.formatDateTime(view.getContext(), timeMillis,
dateFlags));
}
And then use it in your TextView:
<TextView ... app:timeMillis="#{timeVar}" app:dateFlags="#{dateFlags}"/>
A special variable named context is generated for use in binding
expressions as needed. The value for context is the Context from the
root View's getContext(). The context variable will be overridden by
an explicit variable declaration with that name.
In other words, every time you need to pass the context just use "context" as in #{Object.method(context)}.
To used string resources use this
this.yourView.getRoot().getResources().getString(R.string.your_string)

Error when trying to load bitmaps

Why do I get the error message The method getResources() is undefined for the type ColorObjectManager?
I use this line to load a Bitmap image:
orange = BitmapFactory.decodeResource(getResources(), R.drawable.pearl_orange);
It's working fine if I'm doing this in another class that I call GameLoop which I make an object of inside the MainActivity class. But it's not working when I trying to do this in the class ColorObjectManager which I make an object of inside the GameLoop class. Do you follow?
Why am I limited to just use this loading part in the GameLoop class and not in the ColorObjectManager class? I thought it would help if I passed the Context to the constructor of ColorObjectManager, but it didn't! I guess I'm missing some knowledge here where I can create objects and not. Can I get some help to sort this out? Thanks!
getResource needs a Context object. If you pass the context to ColorObjectManager you can retrieve resources with context.getResources()
I agree with the answer posted by blackbelt. Pass the activity context to the constructor of ColorObjectManager from your activity class.
new ColorObjectManger(ActivityName.this);
Constructor
Context mContext;
public ColorObjectManager(Context context)
{
this.mContext= context;
}
Then use the context to get resources.
Edit:
If you want to use the context only in your load method
public ColorObjectManager(Context context)
{
load(context);
}
To get access getResources() , Activity context is required. Your ColorObjectManager is not an Activity. So you need to pass the Activity context to this class.

Getting context from getContext() or from constructor as a field

Let's use custom or extending View as an example.
Is it more effective to save Context parameter from constructor as a field, than calling getContext() everywhere (supposing there are, let's say, 10 or more places where it is needed)?
Instead of using getContext() every where, it is better to pass current context as argument in constructor where you wanna to use.
View#getContext() is
class View {
protected Context mContext;
public final Context getContext() {
return mContext;
}
}
and a locally cached implementation:
class X {
private final Context mLocalContext;
public X(Context ctx) {
mLocalContext = ctx;
}
}
Now there is a very small difference when you use mLocalContext instead of getContext(). The JVM can get to the required reference of the context object without having to execute the method (which takes a tiny bit of extra time). That call can't be optimized away since View#mContext is mutable (can change). In the local example it can assume that mLocalContext can't change and optimize the code a little better. [Note: I am not 100% sure about what optimizations are / can be done]
The difference might be measurable if you use the context a lot but in this case it does not matter much. It's still a good idea to cache Objects locally if you need them often. Especially when their (re)construction takes time (e.g. when getContext() would create a new Context() or so).
It looks like, from the source code, the View's constructor stores the context parameter and that's what getContext() returns:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/view/View.java#View.getContext%28%29
So, my instinct would be that storing the parameter yourself would be redundant, since the super class is already doing it for you.

make singleton object using xml in android

Is their any way to make singleton object by using xml.As we know if we write className with package it call the constructor of the class but i want to use this xml in various other xml files using include tag.But it call all the time its consturctor and creating various object of that.
<jp.ne.biglobe.common.CustomSlidingDrawer>
something
</jp.ne.biglobe.common.CustomSlidingDrawer>
it call the constructor of the CustomSlidingDrawer class As i included it several other file.
Please Suggest me how to make it singleton object.
Maybe you could create a normal class, not singleton that you instanciate through xml. All instances would have a static method getView that returns the singleton.
But this would be awfull design. Just a syntactic workaround.
Regards,
Stéphane

Categories

Resources