如何调用一个类的方法在其他包不停方法

2023-09-05 23:57:26 作者:用生活展示人生

我在Android的初学者开发。 可以any1能请指导我如何调用一个类在其他包保存的方法。

I am a beginner in Android Developing. Can any1 please guide me how to call a Method of a class kept under other package.

像A类在包1调用该方法返回一个数组或对象包2的B类的方法。

Like class A in Package 1 calls a method in Class B of Package 2 which returns An array or object.

我要创建一个Intent为?其实我已经收集所有信息,从不同的类1级保持在不同的包。

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.

在此先感谢。

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;

}
}

我要打电话上述其他类的方法,并得到这些阵列。 那怎么办,有没有在上面code ??

I want to call above methods from other class and get these arrays. How to do that, is there any error in above code??

推荐答案

假设,我们谈论了Java ,然后我们有关于调用方法中的一些规则类包等。为了简单起见,这个作品:

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 "};
  }
}

现在你code访问类的方法 ArrayProvider 从其它的包:

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();
  }
}

进一步阅读

Java教程(你的问题针对Java基本知识) The Java Tutorial (Your question targets basic Java knowledge)