Proximity Alert Demo : PendingIntent « Core Class « Android

Home
Android
1.2D Graphics
2.Animation
3.Core Class
4.Database
5.Date Type
6.Development
7.File
8.Game
9.Hardware
10.Media
11.Network
12.Security
13.UI
14.User Event
Android » Core Class » PendingIntent 




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 = (LocationManagerthis.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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.