java - Android 接近警报不工作

标签 java android google-maps proximity

我正在尝试创建一个应用程序,允许您在单击标记的信息窗口时为标记设置接近警报。

    googleMap.setOnInfoWindowClickListener(
            new OnInfoWindowClickListener(){
     public void onInfoWindowClick(Marker marker) {

         LatLng clickedMarkerLatLng = marker.getPosition();
                double lat =  clickedMarkerLatLng.latitude;
                double long1 =  clickedMarkerLatLng.longitude;

            Log.e("hello", "Output=" + lat + long1);

               LocationManager lm;
         //   double lat=123,long1=34;    //Defining Latitude & Longitude
              float radius=30;                         //Defining Radius

            lm=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
            Intent i= new Intent("com.example.sleepertrain5.ProximityReceiver");           //Custom Action
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1, i, PendingIntent.FLAG_CANCEL_CURRENT);
            lm.addProximityAlert(lat, long1, radius, -1, pendingIntent);

     }

这是调用广播接收器的代码

public class ProximityReceiver extends BroadcastReceiver {

 @Override
 public void onReceive(Context arg0, Intent arg1) {
 // TODO Auto-generated method stub
 // The reciever gets the Context & the Intent that fired the broadcast as arg0 & agr1 

 String k=LocationManager.KEY_PROXIMITY_ENTERING;
// Key for determining whether user is leaving or entering 

 boolean state=arg1.getBooleanExtra(k, false);
 //Gives whether the user is entering or leaving in boolean form

 if(state){
 // Call the Notification Service or anything else that you would like to do here
 Toast.makeText(arg0, "Welcome to my Area", 600).show();
 }else{
 //Other custom Notification 
Toast.makeText(arg0, "Thank you for visiting my Area,come back again !!", 600).show();

 }

}

上面是广播接收器。这些都不起作用,我不明白为什么。任何帮助都会很棒。

最佳答案

将以下代码用于待定意向:

PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
                nId, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

其中 nId 将是代表特定未决 Intent 的任何唯一编号。

关于java - Android 接近警报不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15469826/

相关文章:

java - 实例化 SupportMapFragment 随机抛出 NullPointerException

java - 将一个 json 属性映射到 java 中的两个不同字段 (jackson)

java - Android NestedScrollView 具有静态内容滚动限制

Android getResources().getDrawable() 已弃用 API 22

android - 如何将 Android 选择器列表中的应用程序列为电子邮件客户端?

javascript - Zoom_changed - 中心

javascript - 为什么在执行谷歌地图方向服务回调时DomWindow对象是 'this'

java - 通过java查找服务器互联网带宽以进行流式传输

java - 如何在应用程序终止之前将图像保存到磁盘?

java - 使用 Gradle Kotlin DSL 包含脚本