Proximity Alert Demo : PendingIntent « Core Class « Android






Proximity Alert Demo

   
package app.test;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

 class ProximityReceiver 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));
    }
  }
}

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        double lat = 31.334954;    
        double lon = -80.5625;
        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);
        locMgr.addProximityAlert(lat, lon, radius, 2000L, pIntent1);

        lat = 38.54;    
        lon = -80.38;
        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 ProximityReceiver();

        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);
    }
}

//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>

   
    
    
  








Related examples in the same category