How can i access android global variable from java class - android

I need to access android global(Application) variable from java class which is inside the my application.I tried with using Context ,but unable.Is there a way to do this ?
Below is the my Application class.I am adding some values for jsonUrl inside my activity.So i need to access these values from a java class.
public class Application extends Application {
private static String jsonUrl;
public static String getJsonUrl() {
return jsonUrl;
}
public static void setJsonUrl(String jsonUrl) {
SPHApplication.jsonUrl = jsonUrl;
}
}
Then i tried to get this valus using below code in my java class.
Application.getJsonUrl();
But it doesn't give me the valus?

It depends.
You could use a static variable by using the static keyword. Then, by using MyClass.staticVariable, you can access it.
Another way is to subclass the android.app.Application class and store your variables there. Then, retrieve them whenever you need.
How to declare global variables in Android?

Related

How to use FilesDir.Path property outside Activity(Xamarin.Android)?

I create the following class:
public class GlobalVariables
{
public static string databasePath = FilesDir.Path;
private GlobalVariables()
{
}
}
But FilesDir.Path is underlined with red and it doesn't allow me to import its namespaces to use it. When I use it in some Activity I'm able to but when I'm trying to use it in a class like that I'm not able to. With that class I'm trying to get the apk folder path of the project.
How to use FilesDir.Path property outside Activity(Xamarin.Android)?
You need using a Context to implement this feature:
public class GlobalVariables
{
public static string databasePath = Android.App.Application.Context.FilesDir.Path;
...
}
Furthermore, you could refer to: What is 'Context' on Android?

Is it necessary to write URL of webservice in each page

Is it necessary to write URL of web-service in each page in android application or any other way where i can save URL once for the whole application.
I have a android application where i am calling web-service . I am new to android so i don't have idea of saving the URL globally. How i can save the URL once in the whole application.
private final String URL = "http://192.1.1.1/Service1.asmx";
Instead of writing in every .java file what else i can do.
Create a class in java and declare a String field like the following in that class,
public class MyConstants {
public static final String Url = "http://192.1.1.1/Service1.asmx";
}
then you can access it globally from any class like the following
String url = MyConstants.Url;
As Url is a static final field of MyConstants class, so you can access it just with the name of the class(without creating the object of the class with new Operator). i.e MyConstants in this case.
For Learning more about static and to see how this thing works, Please refer to this link
final means that the value cannot be changed after initialization, that's what makes it a constant. static means that instead of having space allocated for the field in each object, only one instance is created for the class.
So, static final means only one instance of the variable no matter how many objects are created and the value of that variable can never change.
If you don't want to create a class you can just put it on res/values/strings
<string name="URLWebService">http://192.1.1.1/Service1.asmx</string>
Then in each class you need it do this :
getResources().getString(R.string.URLWebService);
And you can do it directly or just put a
public static final String URL = getResources().getString(R.string.URLWebService);
You can use what you want all are going to work fine.
You can also use the string file in res/values.
You can you like following:
<string name="ws_url">http://192.1.1.1/Service1.asmx</string>
and use it so: String wsUrl = getString(R.string.ws_url)
create a global constant class and save constants there :
public class Constants {
public static final String Url = "http://192.1.1.1/Service1.asmx";
}
And access it anywhere in application by just calling
Constants.Url
Create common class YourGlobalClass:
public class YourGlobalClass{
public static final String URL = "192.1.1.1/Service1.asmx";
}
And wherever you want just call the class name. your variable name
ex. YourGlobalClass.URL;

Is there any convention for a helper class in Android?

For every Activity I add to my app I'm noticing a lot of similar code being used in the initialization of the Activity. A helper class with a static method to wrap this similar code seems the way to go.
I first thought of a singleton class. I could add static methods/variables and use them across the application. I haven't really tried to see how would this work in an Android application. Searching a little bit more I saw something about creating a class extending Application. For this I did a simple test:
public class MyApp extends Application {
public static String DEMOTEXT = "WORKING!";
public static void ShowToast(Context context, String text) {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
}
}
MyApp.ShowToast(this, MyApp.DEMOTEXT); // Placed on onCreate of some Activity
This works exactly as I expected. Is this the way to go on Android or is there a better convention? Anything else I should consider when doing this?
By the way, should I use the final keyword on the string? What about the method?
EDIT: I just read this:
There is normally no need to subclass Application. In most situation,
static singletons can provide the same functionality in a more modular
way. If your singleton needs a global context (for example to register
broadcast receivers), the function to retrieve it can be given a
Context which internally uses Context.getApplicationContext() when
first constructing the singleton.
http://developer.android.com/reference/android/app/Application.html
Should I use a singleton then?
Application is primarily used for a global application initialization. You would create your own class, override Application.onCreate() and initialize your static application data there.
Dont forget to declare it in the AndroidMainfest.xml:
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
android:name="your.package.path.to.MyApp">
A static helper class is made the way you did.
The convention is to use lower case letter at first position, so MyApp.showToast(...).
You would use final for the String if you would want to avoid madifications on other places (since it should be a contant).
// this would allow ...
public static String DEMOTEXT = "WORKING!";
// ... to do this somewhere else
MyApp.DEMOTEXT = "NOT WORKING!"
I haven't tried this but I think you should be able to do something like this as well.
public class MyActivity extends Activity {
private static final String DEMOTEXT = "WORKING!";
#Override
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
Toast.makeText(this, DEMOTEXT, Toast.LENGTH_SHORT).show();
}
}
Now for all activities that need to use that initialization could just extend your base activity class.
public class SomeActivity extends MyActivity {
...
// Should display the toast on create
...
}
Yes just use a singleton. Well in this case if your methods are static, you don't even need a singleton. Just a class with static methods.

update Global Var From LocationListener

I have created a class which holds global vars:
public class GlobalVar extends Application{
private XData xData;
public XData getxData ()
{
return xData;
}
public void setXdata (XData Xdata)
{
this.xData = xData;
}
}
When I access this class using (GlobalVar)getApplicationContext() from the activities of my application its fine, but when I want to access it from another class (in this case its LocationListener), I cant use (GlobalVar)getApplicationContext()
How could I access the data?
You should implement the Singleton pattern on your GlobalVar class. And access them directly without getter/setter is recommended on android (read the performance guide).
You could also just make your xData variable static and than you can access it directly from everywhere.
By using:
Context.getApplicationContext()
You can call
GlobalVar gv = (GlobalVar) getApplication()
from any Activity in your code.
For more info refer to http://developer.android.com/reference/android/app/Activity.html#getApplication()

Android/Java - How to access a variable declared in another file?

I would like to know if it is possible to access a variable declared in another file. For example:
httpPostFileUpload(client,
"/data/data/fshizzle.com/files/image.jpg",
"http://10.0.2.2/upload.php", "uploaded",
s.getSelectedItem().toString());
Here, I'd like to replace http://10.0.2.2/upload.php with a URL stored in a variable, but with the variable declared in another file. How do I do this in Java?
You can declare in another java file a public static variable which can then be accessed every where else.
For example,
Class1.java
package com.my.app;
public class Class1 {
public static String URL = "http://10.0.2.2/upload.php";
}
Class2.java
package com.my.app;
public class Class2 {
public void Function(){
httpPostFileUpload(client, "/data/data/fshizzle.com/files/image.jpg",
Class1.URL, "uploaded", s.getSelectedItem().toString());
}
}
Class2 can see Class1 because both are in the same package (if they weren't, a simple import Class1; would fix this)
The static keyword means you can use the variable even without having access to an object of the specified class.
Finally, the public keyword allows you to access the variable from outside the class.

Categories

Resources