549
No
class MainActivity
package kr.co.braverokmc.locationproximity;
import android.Manifest;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
TextView textView, textView2;
LocationManager manager;
ArrayList<PendingIntent> pendingList =new ArrayList<PendingIntent>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
textView2 = (TextView) findViewById(R.id.textView2);
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
public void onButton1Clicked(View v) {
//이전것을 지운다.
pendingList.clear();
//아이디 임의로 지정
// 테스트 위도 경도 지정
// 마지막에 -1 하면 적용 되지 않는다
PendingIntent intent = register(1001, 36.222222, 126.222222, 200, -1);
if(intent!=null){
pendingList.add(intent);
}
//두번재 지역
intent= register(1002, 38.222222, 128.222222, 200, -1);
if(intent!=null){
pendingList.add(intent);
}
textView.setText("1001 : 36.222222, 126.222222");
textView2.setText("1002 : 38.222222, 128.222222");
Toast.makeText(this, "2개 지점 등록", Toast.LENGTH_SHORT).show();
}
//인텐트 해제하기
public void onButton2Clicked(View v) {
unregister();
Toast.makeText(this, "2개 지점 해제됨", Toast.LENGTH_SHORT).show();
}
private PendingIntent register(int id, double latitude, double longitude, float radius
, long expiration) {
Intent intent = new Intent("coffeeProximity");
intent.putExtra("id", id);
intent.putExtra("latitude", latitude);
intent.putExtra("longitude", longitude);
//인텐트 대기
//조건에 맞을 때 실행
// 여기서는 근접했을 때 실행
//PendingIntent.FLAG_CANCEL_CURRENT 은 현재 등록된 것이 있으면 등록된것을
//취소하고 현재 이것을 실행
//조건이 맞으면 내가 받겠다.
PendingIntent pendingIntent = PendingIntent.
getBroadcast(this, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return null;
}
manager.addProximityAlert(latitude, longitude, radius, expiration, pendingIntent);
return pendingIntent;
}
private void unregister() {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
for (int i =0; i<pendingList.size(); i++){
PendingIntent intent =pendingList.get(i);
manager.removeProximityAlert(intent);
}
}
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="kr.co.braverokmc.locationproximity.MainActivity">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:id="@+id/textView" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView"
android:layout_marginTop="44dp"
android:id="@+id/textView2" />
<Button
android:text="시작"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:id="@+id/button"
android:onClick="onButton1Clicked"
/>
<Button
android:text="중지"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_alignRight="@+id/button"
android:layout_alignEnd="@+id/button"
android:layout_marginTop="43dp"
android:id="@+id/button2"
android:onClick="onButton2Clicked"
/>
</RelativeLayout>
class CoffeReceiver
package kr.co.braverokmc.locationproximity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class CoffeReceiver extends BroadcastReceiver {
public CoffeReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
if(intent!=null){
// 0 은 디폴트값
int id=intent.getIntExtra("id", 0);
double latitude =intent.getDoubleExtra("latitude", 0.0D);
double longitude=intent.getDoubleExtra("longitude", 0.0D);
Toast.makeText(context, "근접한 커피숍 : "+id +" : " +latitude +" , " +
longitude,Toast.LENGTH_SHORT).show();
}
}
}
권한 설정
AndroidMainfest.xml
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
125강
126강
댓글 ( 4)
댓글 남기기