I am a beginner in Android Developing. Can any1 please guide me how to call a Method of a class kept under other package.
Like class A in Package 1 calls a method in Class B of Package 2 which returns An array or object.
Do i have to create an Intent for that?? actually i have to gather all information in 1 class from different classes kept under different packages.
Thanks in advance.
package com.xyz.Master;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.telephony.CellLocation;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
public class PhoneInfo extends Activity {
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation location = (GsmCellLocation) tm.getCellLocation();
public int cellID, lac,mcc,mnc;
public String imei,manufacturer,model,product;
String[] phoneInfo;
int[] phoneLocationInfo;
public String[] getHandsetInfo()
{
manufacturer = Build.MANUFACTURER;
model = Build.MODEL;
product = Build.PRODUCT;
imei=tm.getDeviceId();
String softwareVersion = tm.getDeviceSoftwareVersion();
phoneInfo = new String[5];
phoneInfo[0]=imei;
phoneInfo[1]=product;
phoneInfo[2]=model;
phoneInfo[3]=manufacturer;
phoneInfo[4]=softwareVersion;
return phoneInfo;
}
public int[] getHandsetLocationInfo()
{
phoneLocationInfo= new int[4];
String networkOperator = tm.getNetworkOperator();
if (networkOperator != null) {
mcc = Integer.parseInt(networkOperator.substring(0, 3));
mnc = Integer.parseInt(networkOperator.substring(3));
}
CellLocation.requestLocationUpdate();
cellID = location.getCid();
lac = location.getLac();
phoneLocationInfo[0]=cellID;
phoneLocationInfo[1]=lac;
phoneLocationInfo[2]=mcc;
phoneLocationInfo[3]=mnc;
return phoneLocationInfo;
}
}
I want to call above methods from other class and get these arrays. How to do that, is there any error in above code??
Assuming, we're talking about the Java package, then we have several rules for calling methods on classes in other packages. To keep it simple, this works:
package com.example.one;
public class ArrayProvider {
public String[] getArray() {
return new String{"I'm ","inside ", "an ", "array "};
}
public static Object getObject() {
return new String{"I'm ","inside ", "an ", "array "};
}
}
Now your code to access the methods of class ArrayProvider from the other package:
package my.namespace.app;
import com.example.one.ArrayProvider; // import the other class
public class MyClass {
public static void main(String[] args) {
// to access the static method
Object result1 = ArrayProvider.getObject();
// to access "the other" method
ArrayProvider provider = new ArrayProvider();
String[] result2 = provider.getArray();
}
}
Further Reading
Just import other package and instantiate the class B and call the function.
package Package1;
import Package2;
Class A {
void callClassBFunction(){
B b = new B(); //In Package 1 we are instantiating B, however the reference is missing, it should be B b = new B();
b.callFunction();
}
package Package2;
public class B { //class B should be public
Object callFunction(){
//do something and return object
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With