반응형
비 활동 클래스 (LocationManager)에서 getSystemService를 사용하려면 어떻게해야합니까?
주요 활동 OnCreate 메서드에서 다른 클래스로 작업을 오프로드하는 데 문제가있어서 무거운 물건을 들어 올리는 데 어려움이 있습니다.
비 활동 클래스에서 getSystemService를 호출하려고하면 예외가 발생합니다.
어떤 도움이라도 대단히 감사하겠습니다 :)
lmt.java :
package com.atClass.lmt;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.location.Location;
public class lmt extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fyl lfyl = new fyl();
Location location = lfyl.getLocation();
String latLongString = lfyl.updateWithNewLocation(location);
TextView myLocationText = (TextView)findViewById(R.id.myLocationText);
myLocationText.setText("Your current position is:\n" + latLongString);
}
}
fyl.java
package com.atClass.lmt;
import android.app.Activity;
import android.os.Bundle;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.content.Context;
public class fyl {
public Location getLocation(){
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
return location;
}
public String updateWithNewLocation(Location location) {
String latLongString;
if (location != null){
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
}else{
latLongString = "No Location";
}
return latLongString;
}
}
컨텍스트를 fyl 클래스에 전달해야합니다.
한 가지 해결책은 fyl
클래스에 대해 다음과 같은 생성자를 만드는 것 입니다.
public class fyl {
Context mContext;
public fyl(Context mContext) {
this.mContext = mContext;
}
public Location getLocation() {
--
locationManager = (LocationManager)mContext.getSystemService(context);
--
}
}
따라서 활동 클래스에서 onCreate
다음과 같이 함수 에 fyl 객체를 만듭니다 .
package com.atClass.lmt;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.location.Location;
public class lmt extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fyl lfyl = new fyl(this); //Here the context is passing
Location location = lfyl.getLocation();
String latLongString = lfyl.updateWithNewLocation(location);
TextView myLocationText = (TextView)findViewById(R.id.myLocationText);
myLocationText.setText("Your current position is:\n" + latLongString);
}
}
당신은 이것을 위해 갈 수 있습니다 :
getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
이 문제를 해결하는 한 가지 방법은 인스턴스에 대한 정적 클래스를 만드는 것입니다. 나는 그것을 AS3에서 많이 사용했다. 나는 안드로이드 개발에서도 나를 위해 훌륭하게 일했다.
Config.java
public final class Config {
public static MyApp context = null;
}
MyApp.java
public class MyApp extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Config.context = this;
}
...
}
그런 다음 컨텍스트에 액세스하거나 Config.context
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = Config.context.getSystemService(context);
I don't know if this will help, but I did this:
LocationManager locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
(LocationManager) ((lmt) new Activity()).getSystemService(context);
Try this code. This will only work if you have launched activity before calling this line of code. As the activity has to created and be running.
반응형
'development' 카테고리의 다른 글
$ routeProvider와 $ stateProvider의 차이점은 무엇입니까? (0) | 2020.05.25 |
---|---|
Linux 공유 라이브러리가 내보내는 함수 목록을 보려면 어떻게합니까? (0) | 2020.05.25 |
주어진 html 문자열에서 선행 및 후행 공백을 제거하는 방법은 무엇입니까? (0) | 2020.05.25 |
주어진 html 문자열에서 선행 및 후행 공백을 제거하는 방법은 무엇입니까? (0) | 2020.05.25 |
ReactJS에서 중첩 객체의 PropType을 어떻게 검증합니까? (0) | 2020.05.25 |