2014-09-14 4 views
3

Android 애플리케이션에서 LAC 및 Cid 코드를 가져와야합니다. 그러나, 나는 그것을 Activity 클래스 내부에서 할 필요가있다.비 액티비티 클래스의 getSystemService

TelephonyManager telephonyManager =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 
GsmCellLocation cellLocation = (GsmCellLocation) telephonyManager.getCellLocation(); 
int cid = cellLocation.getCid(); 
int lac = cellLocation.getLac(); 

그러나, 메소드 setSystemService는 작업 클래스에 존재하는, 내가 클래스에 "활동의 일종"를 보낼 수있는 무언가를 발견하지 않았습니다 : 내가 찾은 코드는 다음입니다. 활동이 없어도 그렇게 할 수 있습니까?

답변

3

호출합니다. constructor 안에 TelephonyManager을 초기화 할 수 있습니다.

예 :

클래스에서

,

public class MyClass { 

private TelephonyManager mTelephonyManager; 

public MyClass(Context context) { 
    mTelephonyManager =(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
} 

} 활동에서

,

public class MyActivity extends Activity { 

.... 

//Initializing class 
MyClass myClass = new MyClass(this); 

.. 
0

Context 개체를 전달하기 만하면 getSystemService()이 호출됩니다. Activity 개체를 Activity extend Context으로 사용할 수도 있습니다.

그런 다음 당신은 클래스의 constructor에 인수로 context를 전달할 수 있습니다 context.getSystemService()

관련 문제