I've been digging around but can't find anything useful.
I'm working on an Android App.
Basically I have a package and there are three java files in it so far; my main screen page, a settings page and what I have called my 'subs.java' where I am putting useful functions, routines.
What I am trying to do is create this 'subs.java' fil开发者_如何转开发e where routines that get used in more than one place can be stored.
So I have my main app page and I have a settings page. Both of these 'pages' need to use these common functions.
So I was going to put them in my 'subs.java' so I don't end up doubling up code.
Where I am stuck is now I have this subs.java file how do I link to it ?
In PHP if I want to use another file I just include it and I have access to all it's functions.
I suppose I am trying to build up a library, but Java is new to me.
How then would I do this in Eclipse/Java please ?
Here's my subs file, with some useful functions that I found else where :
package com.example.helloandroid;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.content.Context;
import android.widget.Toast;
public class Subs extends Activity {
// Read settings
public String ReadSettings(Context context){
FileInputStream fIn = null;
InputStreamReader isr = null;
char[] inputBuffer = new char[255];
String data = null;
try {
fIn = openFileInput("settings.dat");
isr = new InputStreamReader(fIn);
isr.read(inputBuffer);
data = new String(inputBuffer);
Toast.makeText(context, "Settings read",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Settings not read",Toast.LENGTH_SHORT).show();
}
finally {
try {
isr.close();
fIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return data;
}
// Save settings
public void WriteSettings(Context context, String data){
FileOutputStream fOut = null;
OutputStreamWriter osw = null;
try {
fOut = openFileOutput("settings.dat",MODE_PRIVATE);
osw = new OutputStreamWriter(fOut);
osw.write(data);
osw.flush();
Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show();
}
finally {
try {
osw.close();
fOut.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
The closest thing to PHP's include
in Java is import
. Read more about using import
and package
in Java.
If you've created a project as a package then any class using that package can access any other ones. So if you had SomeFileA.java
and it uses package com.something.me
, and SomeFileB.java
that uses package com.something.me
then they can reference each other. Typically Eclipse will auto-add any imports if you just flat out write SomeFileB b = new SomeFileB();
inside SomeFileA. If not you can just do use
import com.something.me.SomeFileB;
You would use the import
statement to import your subs class. Then you can refer to any function in the subs class as subs.functionName()
If subs.java is in the same folder as your other java files, you can simply type import subs
.
Read more about packages and imports at http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter05/packagesImport.html
What about "import" ? You can import a simple class, not only a library...
Put your common routines as static methods in a class ("MyAppHelper" for example), and call them in your "screens" :
MyAppHelper.function1(...)
精彩评论