Skip to content

Instantly share code, notes, and snippets.

@lvandyk
Created January 25, 2016 07:38
Show Gist options
  • Save lvandyk/b611bf11fd1f253cc5c7 to your computer and use it in GitHub Desktop.
Save lvandyk/b611bf11fd1f253cc5c7 to your computer and use it in GitHub Desktop.
DataService1.java
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
detectionHelper = BeaconDetectionHelper.getIntsance(BeaconDetectionService.this);
beaconManager = detectionHelper.getBeaconManager();
beaconManager.connect(this);
beaconManager.setMonitoringListener(new BeaconManager.MonitoringListener() {
@Override
public void onEnteredRegion(Region region, List<Beacon> beacons) {
try {
beaconManager.startRanging(region);
} catch (RemoteException e) {
e.printStackTrace();
Log.i(TAG, e.getMessage());
}
Intent i = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);
Notification.Builder notification = new Notification.Builder(getApplicationContext())
.setContentTitle(getResources().getString(R.string.welcome_message))
.setContentText(getResources().getString(R.string.welcome_message))
.setSmallIcon(R.mipmap.ic_launcher_two)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
Notification not = notification.build();
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0, not);
}
@Override
public void onExitedRegion(Region region) {
try {
beaconManager.stopRanging(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
return START_STICKY;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment