2

I'm working hard to understand these concepts how they are working.

can someone explain these concepts to me??

here is the code I want to understand

Intent intent = new Intent();
        intent.setAction("com.example.akshay.proximityalertexample2");
        PendingIntent intent1 = PendingIntent.getBroadcast(this, 0, intent, 0);
        locationManager.addProximityAlert(LAT, LONG, 200, -1, intent1);
        IntentFilter filter = new IntentFilter("com.example.akshay.proximityalertexample2");
        registerReceiver(new ProximityAlert(), filter);

Broadcast class file

package com.example.akshay.proximityalertexample2;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by Akshay on 9/18/2015.
 */
public class ProximityAlert extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String key = LocationManager.KEY_PROXIMITY_ENTERING;
        Toast.makeText(context, key, Toast.LENGTH_SHORT).show();
        Boolean entering = intent.getBooleanExtra(key, false);

        if (entering) {
            Log.e(getClass().getSimpleName(), "entering");

        } else {
            Log.e(getClass().getSimpleName(), "exiting");
        }
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);

        Notification notification = createNotification();

        PendingIntent pendingIntent = PendingIntent.getActivity(context , 0 , null , 0 );

        notification.setLatestEventInfo(context,"Proximity Alert!!","You Are Near the Point of intereste " ,pendingIntent);

    }

    private Notification createNotification() {
        Notification notification = new Notification();
        notification.when = System.currentTimeMillis();
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;

        notification.ledOffMS = 1500;
        notification.ledOnMS = 1500;
        return notification;

    }
}

Please explain me the working of these concepts.. any help would mean me alot

thanks a lot

Akshay Sood
  • 152
  • 1
  • 3
  • 15
  • Whats your understandings ? Let us know first – Syed Nazar Muhammad Sep 18 '15 at 05:02
  • For intents & Pending Intents : http://stackoverflow.com/questions/2808796/what-is-an-android-pendingintent – Syed Nazar Muhammad Sep 18 '15 at 05:04
  • I know that Intents can be used to start other activities , passing data to them. Pending Intents are used to fire an intent at a particular time or event. Broadcast Receivers are used to perform specific task when something happened like when we change the airplane mode to on or off like that. I don't know intent filter. Please explain how they are working step by step – Akshay Sood Sep 18 '15 at 05:06
  • I can elaborate with an example : suppose you have to share an image on social media, at that time you will use intent filter because you don't know that whether user want to share that image on Facebook or twitter or Google+, if you use intent filter when user press the share button it will open options for him to choose by him self where to share that's what intent filter do. – Syed Nazar Muhammad Sep 18 '15 at 05:15
  • http://stackoverflow.com/questions/3172151/what-are-intent-filters-exactly – Syed Nazar Muhammad Sep 18 '15 at 05:19
  • @SyedNazarMuhammad can you Explain Whats going on in the code that I have mentioned about in the question? – Akshay Sood Sep 18 '15 at 06:48

1 Answers1

0

This is a really old thread, but I figured for my own learning and the benefit of anyone else arriving on this thread I'd take a shot at it.

  1. In the first snippet, you are creating an intent that doesn't do anything, and wrapping it with a pendingIntent, to be called by the app later at some point. The intent filter serves to let other apps (mainly the receiver in this case) know what kind of intent are we dealing with (it's a poor example, would've personally gone with something like PROX_ALERT_INTENT). We add a proximity alert, and register the intent with our Receiver class.

  2. In the Receiver class, We first define the boolean key that determines if we've stepped in the proximity alert radius or not. Then we fetch the key from the broadcasted intent (its passed automatically), following which we check if the user has indeed entered the proximity alert area. If he has, we create a notification that has a pending intent attached (which in this case does nothing but could launch a new activity for example), and launch the notification.

Note that this method of notification building has deprecated, have to use the builder now.

A.Xu
  • 121
  • 14