Android : AddProxImityAlert LocationManager 메서드는 방송을 보내지 않습니다
-
21-12-2019 - |
문제
AddProxImityAlert 메서드를 사용하는 Android 프로젝트에서 작업하고 있습니다. 이미이 방법을 알고 있으므로 위치 (위도, 경도) 및 주어진 반경에 의해 주어진 위치에 대한 근접 경고를 설정할 수 있으며 알림당신이 너무 가깝다면 당신은 당신에게.
그래서 나는 3 일 전에 그 일을하고 있었고 나는 다시 같은 문제를 다시 얻고있었습니다.
Bref : 이것은 나의 간단한 코드입니다.
# mainactivity.java
package com.example.proximityalert;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity implements LocationListener {
LocationManager lm;
//Defining Latitude & Longitude
double lat=37.422006 ,long1=-122.084095;
//Defining Radius
float radius=1000;
//Intent Action
String ACTION_FILTER = "com.example.proximityalert";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//i'm registering my Receiver First
registerReceiver(new ProximityReciever(), new IntentFilter(ACTION_FILTER));
//i'm calling ther service Location Manager
lm=(LocationManager) getSystemService(LOCATION_SERVICE);
//for debugging...
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this);
//Setting up My Broadcast Intent
Intent i= new Intent(ACTION_FILTER);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), -1, i, 0);
//setting up proximituMethod
lm.addProximityAlert(lat, long1, radius, -1, pi);
}
@Override
//just For debugging to See the distance between my actual position and the aproximit point
public void onLocationChanged(Location newLocation) {
Location old = new Location("OLD");
old.setLatitude(lat);
old.setLongitude(long1);
double distance = newLocation.distanceTo(old);
Log.i("MyTag", "Distance: " + distance);
}
@Override
public void onProviderDisabled(String arg0) {}
@Override
public void onProviderEnabled(String arg0) {}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
}
.
# proximityReceiver.java
package com.example.proximityalert;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.util.Log;
import android.widget.Toast;
public class ProximityReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Key for determining whether user is leaving or entering
String key = LocationManager.KEY_PROXIMITY_ENTERING;
//Gives whether the user is entering or leaving in boolean form
boolean state = intent.getBooleanExtra(key, false);
if(state){
// Call the Notification Service or anything else that you would like to do here
Log.i("MyTag", "Welcome to my Area");
Toast.makeText(context, "Welcome to my Area", Toast.LENGTH_SHORT).show();
}else{
//Other custom Notification
Log.i("MyTag", "Thank you for visiting my Area,come back again !!");
Toast.makeText(context, "Thank you for visiting my Area,come back again !!", Toast.LENGTH_SHORT).show();
}
}
}
.
* # 문제가 프로그램을 실행할 때, Broadcastreceiver (proximityReciever)가 근접 지점에 가까운 매우 중요한 경우에도 Broadcastreceiver (proximityReciever)가 시스템에 의해 호출되지 않아도 디버거가 두 위치 사이의 거리를 보여 주더라도<1000m : /
입니다해결책
이 주제에 대해 뭔가를 알아 냈습니다. 그리고 AddProximityAlert가 작동하지 않는 이유는 무엇이든, 일부 사람들이 전에 똑같은 질문을했는지 알아 차리고 대답을 얻지 못하고 있습니다!
대답은 나 앞에서 였지만 나는 그것에 관심을 기울이지 않았기 때문에 안드로이드 officiel 문서를 읽을 때 ( 여기 에이 문장" 위치 추정의 근사 성격으로 인해 장치가 주어진 영역을 간단히 통과하면 의도가 발생하지 않을 가능성이 있습니다 ""
솔루션은 실제 장치에서 앱을 테스트하거나 DDMS와 함께 Zone 원하는 때까지 좌표를 매우 천천히 변경하려고 시도합니다.
제휴하지 않습니다 StackOverflow