안드로이드 개발 질문/답변
(글 수 45,052)
메인에서 스레드를 t1이라 지정하고
t1.start()로 스레드를 시작시켰습니다.
그리고 그 스레드에서 2가지 값(위도 경도)를 받아오는 소스인데
스레드 내에 double형 리턴함수를 만들고 그 함수를 콜하려니
t1. 을 쳐 검색하니 검색이 안되더군요
스레드 내 함수를 부를수 있는 방법이 있으면 공유좀 부탁합니다 ㅠ
또 혹시 메인엑티비티 페이지가 아닌 다른 클레스에서 Button을 findViewById
할수 있는지도 궁금합니다''( coordinate 에서 버튼이나 textview를 findViewById해주는게 더 편할듯 하여 묻습니다)
1. 메인페이지( 버튼 클릭시 스레드가 돌아갑니다.)
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Thread t1 = new Thread(new coordinate());
t1.start();
}
});2. 스레드 내용
public double P_Latitude()
public double P_Longitude()이 함수를 부를 예정입니다.
package com.example.client;
import java.io.IOException; import java.util.List; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.location.Address; import android.location.Criteria; import android.location.Geocoder; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.util.Log; import android.widget.TextView;
public class coordinate extends Activity implements Runnable {public LocationManager lm;
private LocationListener locListenD;
double Latitude =0.0,Longitude=0.0; public void run() {
// TODO Auto-generated method stub
String Area=null;
while(true)
{
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
alertCheckGPS();
}
Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.NO_REQUIREMENT);
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(true); String provider = lm.getBestProvider(criteria, true); Location loc = lm.getLastKnownLocation(provider);
if (loc != null) { Latitude=loc.getLatitude();
Longitude = loc.getLongitude(); locListenD = new DispLocListener();
//이 부분은 좌표가 변했을 시 좌표를 변화시켜주는 부분이다.
lm.requestLocationUpdates( provider/*"gps"*/, 30000L, 10.0f, locListenD);
}
else { Log.d("location", "location is null");
}
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("Thread1 error !!! \n error code: "+e);
}
}
}
private void alertCheckGPS() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("GPS가 실행중이지 않습니다 GPS페이지로 이동합니다.")
.setCancelable(false)
.setPositiveButton("GPS연걸 페이지",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
moveConfigGPS();
}
})
.setNegativeButton("취소",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
private void moveConfigGPS() {
Intent gpsOptionsIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(gpsOptionsIntent);
}
protected class DispLocListener implements LocationListener {
public void onLocationChanged(Location location) {
// TextView를 업데이트 한다.
//tvLatitude.setText(Double.toString(location.getLatitude()));
//tvLongitude.setText(Double.toString(location.getLongitude()));
Latitude = location.getLatitude();
Longitude = location.getLongitude();
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) { }
}
public double P_Latitude()
{
return Latitude;
}
public double P_Longitude()
{
return Longitude;
}
}



1. 스레드 내 함수를 부를수 있는 방법이 있으면 공유좀 부탁합니다
보통은 스레드에 함수를 만들어서 호출하는게 아닌 스레드내에서 생긴 결과를 스레드 외부의 메소드 혹은 변수에 저장하게 합니다.
스레드는 로직수행이 완료가되면 메모리 해제가 되어버릴수 있기 때문에 방식을 바꾸시는걸 추천합니다.
핸들러 혹은 외부의 변수에 set메소드를 통해 저장하시는것을 고려해보시기 바랍니다.
2. 메인엑티비티 페이지가 아닌 다른 클레스에서 Button을 findViewById
findViewById 는 LayoutInflater를 이용해 레이아웃을 객체화 시킨후에 그 객체.findViewById를 호출하시면 가능합니다.
하지만 원하는 객체를 얻을수 있는지는 잘 모르겠습니다.