Java tutorial
/** * Copyright 2016 Google Inc. All Rights Reserved. * <p/> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p/> * http://www.apache.org/licenses/LICENSE-2.0 * <p/> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.android.gms.nearby.messages.samples.hellobeacons; import android.app.IntentService; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.support.v4.app.NotificationCompat; import android.text.TextUtils; import android.util.Log; import com.google.android.gms.nearby.Nearby; import com.google.android.gms.nearby.messages.Message; import com.google.android.gms.nearby.messages.MessageListener; import java.util.List; import java.util.Timer; import java.util.TimerTask; //Created by bruno.klein on 08/11/2016. /** * While subscribed in the background, this service shows a persistent notification with the * current set of messages from nearby beacons. Nearby launches this service when a message is * found or lost, and this service updates the notification, then stops itself. */ public class BackgroundSubscribeIntentService extends IntentService { private static final String TAG = "BackSubIntentService"; Timer timer = new Timer(""); boolean canDelete = true; private static final int MESSAGES_NOTIFICATION_ID = 1; public BackgroundSubscribeIntentService() { super("BackgroundSubscribeIntentService"); } @Override public void onCreate() { super.onCreate(); } @Override protected void onHandleIntent(Intent intent) { if (intent != null) { Nearby.Messages.handleIntent(intent, new MessageListener() { @Override public void onFound(Message message) { canDelete = false; timer.cancel(); timer.purge(); Utils.saveFoundMessage(getApplicationContext(), message); updateNotification(); Log.w(TAG, "Message onFound: " + message); } @Override public void onLost(Message message) { Utils.removeLostMessage(getApplicationContext(), message); canDelete = true; timer.schedule(new TimerTask() { @Override public void run() { if (canDelete) { Log.i("Script", "This'll run 10 seconds later"); removeNotification(); } } }, 10000); } }); } } private void updateNotification() { List<String> messages = Utils.getCachedMessages(getApplicationContext()); NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE); Intent launchIntent = new Intent(getApplicationContext(), MainActivity.class); launchIntent.setAction(Intent.ACTION_MAIN); launchIntent.addCategory(Intent.CATEGORY_LAUNCHER); PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT); String contentTitle = getContentTitle(messages); String contentText = "Deseja abrir a porta?"; NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_digitaldesk_big).setContentTitle(contentTitle) .setContentText(contentText).setStyle(new NotificationCompat.BigTextStyle().bigText(contentText)) .setOngoing(false).setContentIntent(pi); notificationManager.notify(MESSAGES_NOTIFICATION_ID, notificationBuilder.build()); } private void removeNotification() { NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE); notificationManager.cancel(MESSAGES_NOTIFICATION_ID); } private String getContentTitle(List<String> messages) { switch (messages.size()) { case 0: return getResources().getString(R.string.scanning); case 1: return getResources().getString(R.string.one_message); default: return getResources().getString(R.string.many_messages); } } }