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.
Related
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?
example:
Why can I write like that MainActivity.this.getContentResolve();
but can not write like that this.getContentResolve(); in MainActivity.java
If you need to access instance of enclosing class from inner class you need to make declaration like this - ClassName.this.anyClassMethod();
For more info read this article Nested Classes
This syntax becomes relevant when using inner classes.
public class A {
String str = "A";
public class B {
String str = "B";
public String getStr() {
return A.this.str; //returns A
}
}
}
It's long described but i think your question is related to anonymous class.
When you are inside class and want to refer to the current object of the class you can use this for example:
public class MyActivity extends Activity{
int foo;
public Test(int _foo){
this.foo = _foo;
}
}
but when you want to refer to the current class object from anonymous class inside it you should use class.this for example:
MyActivity.this
Full example for Inner Class:
public class Test {
int foo = 1;
public class InnerTest {
public String getFoo() {
return Test.this.foo;
}
}
}
Why can I write like that MainActivity.this.getContentResolve() but
can not write like that this.getContentResolve()?
Because your trying to access the context of outer class (MainActivity) in the inner class. we use TheActivityClassName.this in the inner class to access the outer TheActivityClassName class’s context.
When we are accessing the activity context in inner class we need a reference to the activity class name so we pass it like MainActivity.this
and when we need it in the class then we can reference it simply like this.something
You should have a look here to get good grasp on what context is actually
Hope it helps
There is no difference if you are calling getContentResolver() from any direct method of the activity. You can write both MainActivity.this.getContentResolver(); and this.getContentResolver(); as well as simply getContentResolver() with the same effect. In this case, the this keyword refers to the current instance of the MainActivity.
However, if you are within an inner class or inside an implementation of an interface/abstract method inside the MainActivity, then this will refer to an instance of the inner class or the interface you are implementing. In that case, you have to call MainActivity.this to get access to the instance of the MainActivity.
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;
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?
Is there a way to use the getString method from a seperate class?
I have a string stored in my strings xml, I'd like to use that string in an object... but the method isn't even available in the object...
any tips?
getString() is a method of the Context class¹. If you need it inside a seperate class (that does not extend Context), it's usually best to provide it as a seperate argument to the method that needs it.
Example:
public void logString(Context c, int stringId) {
Log.d("TAG", c.getString(stringId));
}
One thing is important: Never store the context inside the separate class.
Provide an argument. Otherwise you will leak memory and disrupt the whole android lifecycle if the object that stores the context lives longer than the object where the context originally belongs to (e.g. an activity).
¹ getString() can also be used from the Resources class - which you can get via Context.getResources()
the solution here is to make sure your object has a reference to the application context
Class Swag{
private Context ctx;
public Swag(Context ctx){
this.ctx = ctx;
}
public void doSomething(){
String something = ctx.getResources().getString(R.string.somestring);
...
}
// or like this
public void makeUpperCase(Context appContext){
appContext.getResources().getString(R.string.super_string_swag_yolo);
}
}
obviously you'd have to supply the context when creating an object or when caling the method
resouce file: values/strings.xml
<resources>
<string name="app_name">App name</string>
<resources>
java
import android.content.res.Resources;
Resources.getSystem().getString(R.string.app_name);//result : App name
edit:
The below will NOT work. I read this on another site and assumed it worked, but I just tried it in my app and kept getting an error. Problem is, it will compile but you will get a runtime exception.
This will work from any java class:
import android.content.res.Resources
Resources.getSystem().getString(R.string.blah);
if you cannot pass a context as parameter, create another class, where you put all your static data.
example :
public class StaticData {
public static String BASE_URL = "https://stackoverflowrocks.com";
}
and get that string from your other class by calling directly
StaticData.BASE_URL
nice and clean.
This works, but for SYSTEM resources only:
import android.content.res.Resources
Resources.getSystem().getString(R.string.blah);
Reference: https://stackoverflow.com/a/40917607/8994882
Try this in your java file:
String myString = getResources().getString(R.string.MY_STRING)
Now use this string object.