LocationManager.KEY_PROXIMITY_ENTERING无法正常工作,什么也不显示?

时间:2018-11-28 06:56:12

标签: android

我想开发一个Android应用程序,用于检查用户是否在建筑物范围内。用户输入位置时如何显示Toast消息。

这是我的代码: Main Activity.java

public class MainActivity extends Activity {
private final String PROX_ALERT = "app.test.PROXIMITY_ALERT";
private ProximityReciever proxReceiver = null;
private LocationManager locMgr = null;
PendingIntent pIntent1 = null;
PendingIntent pIntent2 = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    double lat = 27.7172453;
    double lon = 85.3239605;
    float radius = 5.0f * 1609.0f;
    String geo = "geo:" + lat + "," + lon;
    Intent intent = new Intent(PROX_ALERT, Uri.parse(geo));
    intent.putExtra("message", "Jacksonville, FL");
    pIntent1 = PendingIntent.getBroadcast(getApplicationContext(), 0, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    locMgr = (LocationManager) this.getSystemService(LOCATION_SERVICE);
    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;
    }
    locMgr.addProximityAlert(lat, lon, radius, 2000L, pIntent1);

    lat = 27.7172453;
    lon = 85.3239605;
    geo = "geo:"+lat+","+lon;

    intent = new Intent(PROX_ALERT, Uri.parse(geo));
    intent.putExtra("message", "Orlando, FL");

    pIntent2 = PendingIntent.getBroadcast(getApplicationContext(), 0, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    locMgr.addProximityAlert(lat, lon, radius, 60000L, pIntent2);

    proxReceiver = new ProximityReciever();

    IntentFilter iFilter = new IntentFilter(PROX_ALERT);
    iFilter.addDataScheme("geo");

    registerReceiver(proxReceiver, iFilter);
}

protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(proxReceiver);
    locMgr.removeProximityAlert(pIntent1);
    locMgr.removeProximityAlert(pIntent2);
}
}

这是我的ProximityReciever.java

 public class ProximityReciever extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent intent) {
    if(intent.getData() != null)
        Log.v(TAG, intent.getData().toString());
    Bundle extras = intent.getExtras();
    if(extras != null) {
        Log.v("", "Message: " + extras.getString("message"));
        Log.v("", "Entering? " + extras.getBoolean(LocationManager.KEY_PROXIMITY_ENTERING));
    }
}

我做错了什么,请帮帮我。谢谢。 我只想将我的房屋设置为静态位置,并检查设备何时进入我所在位置的半径。

0 个答案:

没有答案