Skip to content

Instantly share code, notes, and snippets.

@wicksome
Last active August 29, 2015 14:05
Show Gist options
  • Save wicksome/35723354cd6c66ce8d76 to your computer and use it in GitHub Desktop.
Save wicksome/35723354cd6c66ce8d76 to your computer and use it in GitHub Desktop.
안드로이드에서 gps 사용 코드, 데몬스레드 사용
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.yeongjun_lab.myapplication.R;
public class GPSOnSecondThread extends Activity {
final static String TAG_NAME = "GPSTestActitity";
private TextView tvLat, tvLon;
private Button btnStart, btnStop, btnRefresh;
private double gps_latitude = 0;
private double gps_longtitude;
private double gps_current_speed = 0;
private double gps_accuracy = 0;
private double gps_altitude = 0;
Context mContext;
BackThread thread = null;
private HandlerThread mThread;
private LocationManager mLocationManager;
private final LocationListener mLocationListener = new
LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.i(TAG_NAME, "onLocationChanged, location = " +
location);
gps_latitude = location.getLatitude();
gps_longtitude = location.getLongitude();
gps_current_speed = location.getSpeed();
gps_accuracy = location.getAccuracy();
gps_altitude = location.getAltitude();
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle
extras) {
}
};
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gps_test);
tvLat = (TextView) findViewById(R.id.tv_latitude1);
tvLon = (TextView) findViewById(R.id.tv_longtitude1);
btnStart = (Button) findViewById(R.id.btn_start1);
btnStop = (Button) findViewById(R.id.btn_stop1);
btnRefresh = (Button) findViewById(R.id.btn_refresh1);
mContext = getApplicationContext();
mLocationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
tvLat.setText("초기값");
tvLon.setText("초기값");
btnRefresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// onThreadStart();
}
});
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final boolean gpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!gpsEnabled) {
showGPSSettingsDialog();
} else {
long minTime = 0;
float minDistance = 0;
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, mLocationListener);
onGPSStart(v);
}
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onGPSStop(v);
mLocationManager.removeUpdates(mLocationListener);
// mThread.getLooper().quit();
}
});
}
class BackThread extends Thread {
public void run() {
while (true) {
mHandler.sendEmptyMessage(0);
try {
BackThread.sleep(500);
} catch (InterruptedException e) {
break;
} finally {
}
}
}
}
// 스레드간의 통신장치
// main ui 는 다른 thread에서 변경X
// 그렇기 때문에 handler 사용.
public Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
tvLat.setText(Double.toString(gps_latitude));
tvLon.setText(Double.toString(gps_longtitude));
Log.i(TAG_NAME, "handleMessage, location = " +
gps_latitude + "," + gps_longtitude);
}
};
public void onGPSStart(View v) {
Log.i(TAG_NAME,"onGPSStart");
if (thread == null || thread.getState() == Thread.State.TERMINATED) {
thread = new BackThread();
thread.setDaemon(true);
thread.start();
Log.i(TAG_NAME, "onGPSStart-thread.start()");
} else if (thread.getState() == Thread.State.BLOCKED || thread.getState() == Thread.State.WAITING || thread.getState() == Thread.State.TIMED_WAITING
|| thread.getState() == Thread.State.RUNNABLE) {
thread.notifyAll();
Log.i(TAG_NAME, "onGPSStart-thread.notifyAll()");
}
}
public void onGPSStop(View v) {
Log.i(TAG_NAME, "onGPSStop");
if (thread.getState() == Thread.State.RUNNABLE || thread.getState() == Thread.State.TIMED_WAITING || thread.getState() == Thread.State.BLOCKED
|| thread.getState() == Thread.State.WAITING)
thread.interrupt();
Log.i(TAG_NAME, "onGPSStop-thread.interrupt()");
// android.os.Process.killProcess(android.os.Process.myPid());
}
public void showGPSSettingsDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("GPS 활성화 요청");
alertDialog.setMessage("현재 GPS가 비활성화 되어있습니다.");
// Setting Icon to Dialog
// alertDialog.setIcon(R.drawable.delete);
// On pressing Settings button
alertDialog.setPositiveButton("설정",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
}
);
alertDialog.setNegativeButton("취소",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}
);
alertDialog.show();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (thread == null) {
} else if (thread.getState() == Thread.State.RUNNABLE || thread.getState() == Thread.State.TIMED_WAITING)
thread.interrupt();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment