简体   繁体   English

接近警报类别不会触发Android

[英]Proximity Alert Class Doesn't Fire Android

I'm trying to set up a proximity alarm based on the location of a selected marker, the coordinates for which are stored in an external file and read into an array, which in turn draws the coordinates. 我正在尝试根据所选标记的位置设置接近警报,其标记的坐标存储在外部文件中,并读取到数组中,后者依次绘制坐标。

    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=0;
 //  double long1=0;    //Defining Latitude & Longitude
     float radius=3000;


    lm=(LocationManager) getSystemService(LOCATION_SERVICE);
    Intent i= new Intent("com.example.sleepertrain5.proximityalert");           //Custom Action
    PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), -1, i, 0);
    lm.addProximityAlert(lat, long1, radius, -1, pi);
    Toast.makeText(getBaseContext(), 
            "Info Window clicked@" + lat + "dddjdj" + long1, 
            Toast.LENGTH_SHORT).show();



    class ProximityReceiver extends android.content.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();

      }

    }

    }
            }

});
}

The coordinates of the selected marker are passed to the location manager, but the proximity alarm doesn't work. 所选标记的坐标将传递到位置管理器,但是接近警报不起作用。 Obviously at the moment, it just displays a toast, but it's not even doing that. 显然,目前,它只是显示一个吐司,但它甚至没有这样做。 According to the Log, the Proximity receiver class is never called, but I can't figure out why. 根据日志,永远不会调用邻近接收器类,但是我不知道为什么。 I've tried it with varying sizes of radius and it still doesn't work. 我尝试了不同大小的半径,但仍然无法正常工作。 Any ideas or help? 有什么想法或帮助吗?

You need to register your BroadcastReceiver with the Intents action you used for the PendingIntent. 您需要使用用于PendingIntent的Intents操作来注册BroadcastReceiver。 You can do this in the AndroidManifest.xml by adding these lines: 您可以在AndroidManifest.xml中通过添加以下行来做到这一点:

<receiver android:name="MyScheduleReceiver" >
    <intent-filter>
        <action android:name="com.example.sleepertrain5.proximityalert" />
    </intent-filter>
</receiver>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM