package kim.android.test;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.location.Criteria;

import android.location.Location;

import android.location.LocationListener;

import android.location.LocationManager;

import android.os.Bundle;

import android.telephony.SmsMessage;

import android.util.Log;

import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver {

Location location;

LocationManager locationManager;

// SMS가 수신되면 호출되는 메서드이다.

@Override

public void onReceive(Context context, Intent intent) {

Bundle bundle = intent.getExtras();

SmsMessage[] msgs = null;

String number="";

String message="";

if (bundle != null) {

Object[] pdus = (Object[]) bundle.get("pdus");

msgs = new SmsMessage[pdus.length];

for (int i=0; i<msgs.length; i++){

msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);

// SMS를 보낸 전화번호를 얻어온다.

number=msgs[i].getOriginatingAddress();

// SMS 내용

message= msgs[i].getMessageBody().toString();

//Log.i("SmsReceiver","number: "+number+ " message: "+message);

// 발신자와 발신메시지를 체크하여 위치정보를 요청하는

// 문자메시지이면 현재 위치정보를 전송한다.

if ( number.equals("01011112222") &&

message.equals("position check")) {

Log.i("SmsReceiver","call sendMyLocation(context)");

checkMyLocation(context);

// 이부분에 서버에 위도,경도값을 전송하는 코드를 작성하면 됩니다.

Log.i("SmsReceiver","Current Location>> latitude: "+

location.getLatitude()+" longitudeL "+

location.getLongitude());

}

}

}

}

// 현재 위치 정보를 체크하는 메서드

// Criteria 클래스를 사용하여 현재 사용가능한 Provider중 가장

// 최적의 Provider를 선택하여 사용하도록 설정

public void checkMyLocation(Context context) {

locationManager =

(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);

Criteria criteria = new Criteria();

criteria.setAccuracy(Criteria.ACCURACY_FINE); // 세밀한 정확도

criteria.setPowerRequirement(Criteria.POWER_HIGH); // 전원 소비량

criteria.setAltitudeRequired(false); // 고도, 높이 값을 얻어 올지를 결정

criteria.setSpeedRequired(false); //속도

criteria.setCostAllowed(false); //위치 정보를 얻어 오는데 들어가는 금전적 비용

String provider = locationManager.getBestProvider(criteria, true);

// 에뮬은 DDMS 작동보다 listener를 먼저 등록해야

// 최초 DDMS의 변화를 받아들일수 있다.

// 10초간격, 100미터 간격 이 생기면 이벤트가 발생했다고 감지

locationManager.requestLocationUpdates(

provider, 10000, 100, new MyLocationListener());

if (provider == null) {

//GPS제공자가 없으면 기지국에서라도 Location 정보 얻어옴.

Toast.makeText(context, "GPS 제공자가 없음!!", 2000).show();

provider = LocationManager.NETWORK_PROVIDER;

location = locationManager.getLastKnownLocation( provider );

}

location = locationManager.getLastKnownLocation( provider );

if (location == null){

try {

Toast.makeText(context,

"에뮬레이터 GPS를 작동해주세요", 10000).show();

Thread.sleep(10000); //에뮬레이터 DDMS 작동시간을 위한 인터벌

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

location = locationManager.getLastKnownLocation( provider );

}

}

// Location정보가 변경되는 이벤트가 발생을 처리한다.

private class MyLocationListener implements LocationListener {

public void onLocationChanged(Location location) {

// TODO Auto-generated method stub

SmsReceiver.this.location = location;

}

public void onProviderDisabled(String provider) {

// TODO Auto-generated method stub

}

public void onProviderEnabled(String provider) {

// TODO Auto-generated method stub

}

public void onStatusChanged(String provider, int status, Bundle extras) {

// TODO Auto-generated method stub

}

}

}

AndroidManifest.xml 파일 설정파일에 추가해 주어야 할 내용들

<receiver android:name=".SmsReceiver">

<intent-filter>

<action android:name="android.provider.Telephony.SMS_RECEIVED"/>

</intent-filter>

</receiver>

<uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-permission android:name="android.permission.RECEIVE_SMS" />

<uses-permission android:name="android.permission.INTERNET"/>




-------------------------------------

인터넷에서 자료 찾은건데요..

이걸 돌리면 에러는 없다고 나오는데 실행하면 바로 어플이 종료됩니다...

어디가 문제인지 좀 제발 알려주세요..ㅠㅠㅠ