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?
Related
I have a library project say "SampleLibrary" which has only java files and does not have any activity in it. And i am using this library in my application say "Sample". I have a method as follows in "Sample Library".
public boolean load(String filePathName )
{
String Work;
Work = GetString( "Sample", No , "", filePathName );
if( 0 == Work.length())
{
Work = GetString( "サンプル", No, "", filePathName);
}
}
Now i want customize this code to get the string value from String.xml or by anoy other means. To use getString(R.String.sample) i need a context. but i don't have any activity in my library.
can any one, please help me how to do that?
Create subclass of Application and set the android:name attribute of your <application> tag in the AndroidManifest.xml
android:name=".MyApplication"
Application class
public class MyApplication extends Application{
private static Context mContext;
#Override
public void onCreate() {
super.onCreate();
mContext = this;
}
public static Context getContext(){
return mContext;
}
}
Now in your class you can use application context like this.
MyApplication.getContext().getResources()
The only reasonable solution is to provide a simple init method with Context as a parameter. It's not possible to access Resources from a static context. You can't use Application as well as your code is enclosed in a library.
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?
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.
When programming for Android sometimes you have to use static methods. But when you try to access you resources in a static method with getString(R.string.text) you'll get an error. Making it static doesn't work.
Does anyone knows a good way around this? The resource files in Android are very helpful for creating things in different languages or making changes to a text.
One way or another, you'll need a Context for that... For static methods this probably means you need to pass along a Context when calling them.
You could use Resources.getSystem().getStringArray(android.R.array.done);
This is how I access resources from inside static methods. Maybe not ideal, but.
First, I extend Application and set some public static field(s), and create a method to initialise them:
public class MyApp extends Application {
// static resources
public static String APP_NAME;
public static void initResources(Context context) {
APP_NAME = context.getResources().getString(R.string.app_name);
}
}
And in my manifest I register the extended Application:
<application
android:name=".MyApp"/>
In my starter activity (MainActivity), I make a call to initialise the static resources:
#Override
protected void onCreate(Bundle savedInstanceState) {
MyApp.initResources(this);
}
Then anywhere in your project, after MainActivity.onCreate(Bundle b) has run, you can call static methods that access your specified static resources:
public static void printAppName() {
Log.w("tag", "my app name: " + MyApp.APP_NAME);
}
Pass in a Context (i.e. Activity) instance as a parameter object to static method. Then invoke getString on the parameter.
The post below gives a tip for creating an Application class to save your current context. Your new Application class will then be accessible from any other static method.
How can I get a resource content from a static context?
One way is you can pass context to your static method.
check this out it definitely works
public class Sounds {
public static MediaPlayer getSoundTouch(Context context){
return MediaPlayer.create(context, R.raw.touch);
}
public static MediaPlayer getSoundLeak(Context context){
return MediaPlayer.create(context, R.raw.leak);
}
public static MediaPlayer getSoundFinish(Context context){
return MediaPlayer.create(context, R.raw.finish);
}
}