New to Android/Java, and I’m playing around with AddProximityAlert(). I have the following block of code below, and keep getting errors. Could someone explain what Eclipse is saying, and how I can resolve the error I’m getting for getBroadcast and registerReceiver?
In the SecondaryActivity.java file, I have 2 broadcast receivers, could that be the reason why?
SecondaryActivity.java:
public class SecondaryActivity extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
.
.
.
lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
float radius = 100f;
long expiration = -1;
final String PROX_ALERT_INTENT = "com.example.test";
Intent intent = new Intent(PROX_ALERT_INTENT);
PendingIntent proximityIntent = PendingIntent.**getBroadcast**(this, 0, intent, 0);
lm.addProximityAlert(latitude, longitude, radius, expiration, proximityIntent);
IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
**registerReceiver**(new ProximityIntentReceiver(), filter);
.
.
.
private class ProximityIntentReceiver extends BroadcastReceiver {
…
getBroadcast: The method getBroadcast(Context, int, Intent, int) in the type PendingIntent is not applicable for the arguments (SecondaryActivity, int, Intent, int)
registerReceiver: The method registerReceiver(SecondaryActivity.ProximityIntentReceiver, IntentFilter) is undefined for the type SecondaryActivity
Because you are extending a BroadcastReceiver, you need to use:
and
to reference a Context class.
Also naming a BroadcastReceiver
SecondaryActivityis a little confusing sinceSecondaryActivityis not an Activity.