Reputation: 1
I’m developing an Android application designed to automatically initiate a phone call whenever I enter a specific geofenced location. I’ve confirmed that both the BroadcastReceiver and the background service components are functioning as expected. The BroadcastReceiver is capable of receiving broadcasts when triggered, and the service is operating without any issues. However, the geofence itself doesn’t seem to activate as intended—it doesn’t trigger upon entering the specified location boundary, and as a result, it fails to send the anticipated broadcast to the BroadcastReceiver.
My code:
public class GeofenceService extends Service {
private static final String TAG = "GeofenceService";
private static final String CHANNEL_ID = "GeofenceChannel";
private GeofencingClient geofencingClient;
private List<Geofence> geofenceList;
private List<String> geofenceIds = new ArrayList<>();
@Override
public void onCreate() {
super.onCreate();
createNotificationChannel();
startForeground(1, getNotification());
// Initialize GeofencingClient and geofences
geofencingClient = LocationServices.getGeofencingClient(this);
geofenceList = new ArrayList<>();
// Add your geofence(s) here
setupGeofences();
Toast.makeText(getApplicationContext(), "Service Started", Toast.LENGTH_SHORT).show();
}
@SuppressLint("MissingPermission")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Service started");
// Start geofencing
addGeofences();
return START_STICKY; // Ensure the service is restarted if killed
}
private Notification getNotification() {
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
return new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Geofencing Service")
.setContentText("Monitoring geofence...")
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build();
}
private void createNotificationChannel() {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"Geofencing Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager = getSystemService(NotificationManager.class);
if (manager != null) {
manager.createNotificationChannel(serviceChannel);
}
}
@Override
public IBinder onBind(Intent intent) {
return null; // We don't provide binding, so return null
}
private void setupGeofences() {
// Define geofence(s) here
geofenceList.add(new Geofence.Builder()
.setRequestId("home") // Unique ID for the geofence
.setCircularRegion(
50.09886,
50.88337,
100
)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
.build());
geofenceIds.add("home");
}
@SuppressLint("MissingPermission")
private void addGeofences() {
GeofencingRequest geofencingRequest = new GeofencingRequest.Builder()
.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
.addGeofences(geofenceList) // Add geofences to the request
.build();
Intent intent = new Intent(this, GeofenceBroadcastReceiver.class); // BroadcastReceiver to handle geofence transitions
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
geofencingClient.addGeofences(geofencingRequest, pendingIntent)
.addOnSuccessListener(aVoid -> Log.d(TAG, "Geofences added successfully"))
.addOnFailureListener(e -> Log.e(TAG, "Failed to add geofences", e));
}
private void removeGeofences(List<String> geofenceIds) {
geofencingClient.removeGeofences(geofenceIds);
Log.d(TAG, "removeGeofences: removed");
}
@Override
public void onDestroy() {
super.onDestroy();
removeGeofences(geofenceIds);
Log.d(TAG, "Service destroyed");
// Clean up any resources here
Toast.makeText(getApplicationContext(), "Service Stopped", Toast.LENGTH_SHORT).show();
}
}
Upvotes: 0
Views: 30