package com.example.trs.locationsender;

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.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.location);

  String s;

  TextView text = (TextView) findViewById(R.id.myTextView);
  
  Button button1 = (Button) findViewById(R.id.BtnSendLoc);
  // button.setOnClickListener(mSendLocListener);
  
  LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

  if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

   createGpsDisabledAlert();

  }

  if (loc != null) {
   double lat = loc.getLatitude();
   double lng = loc.getLongitude();

   s = "위도가" + lat + "이며 \n 경도가 " + lng;

  }

  else
   s = "위치파악 안됨";
  text.setText("현재 위치는 \n" + s + "입니다.");

  
  button1.setOnClickListener(new Button.OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent in = new Intent(MainActivity.this, SMSSender.class);
    startActivity(in);
   }
  });
  

 }

 private void createGpsDisabledAlert() {
  AlertDialog.Builder builder = new AlertDialog.Builder(this);
  builder.setMessage("Your GPS is disabled! Would you like to enable it?")
    .setCancelable(false)
    .setPositiveButton("Enable GPS",
      new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog,
         int which) {
        showGpsOptions();
       }
      })
    .setNegativeButton("Do nothing",
      new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
        dialog.cancel();
       }
      });

  AlertDialog alert = builder.create();
  alert.show();
 }

 private void showGpsOptions() {
  Intent gpsOptionsIntent = new Intent(
    android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  startActivity(gpsOptionsIntent);

 }
 
}

 

보시다시피 GPS로 위도 경도를 받는 텍스트와 버튼 하나로 구성되 있는 소스 코드입니다.

 

자바 파일에는 에러가 뜨는 데 소스코드에는 어떠한 에러도 발견 할 수 없는 상태입니다.

 

도와주십시오~