List of usage examples for android.content Intent FLAG_ACTIVITY_SINGLE_TOP
int FLAG_ACTIVITY_SINGLE_TOP
To view the source code for android.content Intent FLAG_ACTIVITY_SINGLE_TOP.
Click Source Link
From source file:com.tinbytes.simplenotificationapp.SimpleNotificationActivity.java
private Intent getNotificationIntent() { Intent intent = new Intent(this, SimpleNotificationActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); return intent; }
From source file:com.nsqre.insquare.Utilities.PushNotification.MyGcmListenerService.java
/** * Create and show a simple notification containing the received GCM message. * Manages how the notification will be shown in the notification bar. * @param message GCM message received./*w w w .ja va2 s . com*/ */ private void sendNotification(String message, String squareName, String squareId) { Intent intent = new Intent(this, BottomNavActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); SharedPreferences notificationPreferences = getSharedPreferences("NOTIFICATION_MAP", MODE_PRIVATE); int notificationCount = 0; int squareCount = notificationPreferences.getInt("squareCount", 0); if (squareId.equals(notificationPreferences.getString("actualSquare", ""))) { return; } if (!notificationPreferences.contains(squareId)) { notificationPreferences.edit().putInt("squareCount", squareCount + 1).apply(); } notificationPreferences.edit().putInt(squareId, notificationPreferences.getInt(squareId, 0) + 1).apply(); for (String square : notificationPreferences.getAll().keySet()) { if (!"squareCount".equals(square) && !"actualSquare".equals(square)) { notificationCount += notificationPreferences.getInt(square, 0); } } squareCount = notificationPreferences.getInt("squareCount", 0); Log.d(TAG, notificationPreferences.getAll().toString()); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this); notificationBuilder.setSmallIcon(R.drawable.nsqre_map_pin_empty_inside); notificationBuilder.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorAccent)); if (squareCount == 1) { SharedPreferences messagePreferences = getSharedPreferences(squareId, MODE_PRIVATE); intent.putExtra("squareId", squareId); NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); messagePreferences.edit().putString(String.valueOf(notificationCount), message).commit(); if (messagePreferences.getAll().size() <= 6) { for (int i = 1; i <= messagePreferences.getAll().keySet().size(); i++) { inboxStyle.addLine(messagePreferences.getString(String.valueOf(i), "")); } } else { for (int i = messagePreferences.getAll().size() - 6; i <= messagePreferences.getAll().keySet() .size(); i++) { inboxStyle.addLine(messagePreferences.getString(String.valueOf(i), "")); } } notificationBuilder.setContentTitle(squareName); notificationBuilder.setStyle(inboxStyle.setBigContentTitle(squareName).setSummaryText("inSquare")); notificationBuilder.setContentText( notificationCount > 1 ? "Hai " + notificationCount + " nuovi messaggi" : message); } else { intent.putExtra("map", 0); intent.removeExtra("squareId"); notificationBuilder.setContentTitle("inSquare"); notificationBuilder .setContentText("Hai " + (notificationCount) + " nuovi messaggi in " + squareCount + " piazze"); } notificationBuilder.setAutoCancel(true); notificationBuilder.setSound(defaultSoundUri); notificationBuilder.setVibrate(new long[] { 300, 300, 300, 300, 300 }); notificationBuilder.setLights(Color.RED, 1000, 3000); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_CANCEL_CURRENT); notificationBuilder.setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE); updateSquares("", "", "update"); SharedPreferences mutePreferences = getSharedPreferences("NOTIFICATION_MUTE_MAP", MODE_PRIVATE); if (mutePreferences.contains(squareId)) { String expireDate = mutePreferences.getString(squareId, ""); long myExpireDate = Long.parseLong(expireDate, 10); if (myExpireDate < (new Date().getTime())) { mutePreferences.edit().remove(squareId).apply(); notificationManager.notify(0, notificationBuilder.build()); } } else { notificationManager.notify(0, notificationBuilder.build()); } }
From source file:org.bfr.periodicquery.PeriodicQueryService.java
@Override public int onStartCommand(Intent intent, int flags, int startId) { // The intent to launch when the user clicks the expanded notification Intent launchIntent = new Intent(this, StartStopActivity.class); launchIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent pendIntent = PendingIntent.getActivity(this, 0, launchIntent, 0); // This constructor is deprecated. Use Notification.Builder instead Notification notice = new Notification(R.drawable.ic_launcher, "Periodic Query", System.currentTimeMillis()); // This method is deprecated. Use Notification.Builder instead. notice.setLatestEventInfo(this, "Periodic Query", "Periodic Query", pendIntent); notice.flags |= Notification.FLAG_NO_CLEAR; startForeground(1234, notice);/*from ww w . ja v a2 s . co m*/ return START_STICKY; }
From source file:no.ntnu.wifimanager.GCMIntentService.java
/** * Issues a notification to inform the user that server has sent a message. *//*from w w w . j a va2 s .co m*/ private static void generateNotification(Context context, String message) { int icon = R.drawable.ic_stat_gcm; long when = System.currentTimeMillis(); NotificationManager notificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(icon, message, when); String title = context.getString(R.string.app_name); Intent notificationIntent = new Intent(context, ClientListActivity.class); // set intent so it does not start a new activity notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0); notification.setLatestEventInfo(context, title, message, intent); notification.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(0, notification); }
From source file:com.fn.reunion.app.xmpp.Notifier.java
public void notify(String notificationId, String apiKey, String title, String message, String uri) { Log.d(LOGTAG, "notify()..."); Log.d(LOGTAG, "notificationId=" + notificationId); Log.d(LOGTAG, "notificationApiKey=" + apiKey); Log.d(LOGTAG, "notificationTitle=" + title); Log.d(LOGTAG, "notificationMessage=" + message); Log.d(LOGTAG, "notificationUri=" + uri); if (isNotificationEnabled()) { // Show the toast if (isNotificationToastEnabled()) { Toast.makeText(context, message, Toast.LENGTH_LONG).show(); }//from www . j a v a2 s . c o m Notification notification = new Notification(); notification.icon = getNotificationIcon(); notification.defaults = Notification.DEFAULT_LIGHTS; if (isNotificationSoundEnabled()) { notification.defaults |= Notification.DEFAULT_SOUND; } if (isNotificationVibrateEnabled()) { notification.defaults |= Notification.DEFAULT_VIBRATE; } notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.when = System.currentTimeMillis(); notification.tickerText = message; Intent intent = new Intent(context, NotificationDetailsActivity.class); intent.putExtra(Constants.NOTIFICATION_ID, notificationId); intent.putExtra(Constants.NOTIFICATION_API_KEY, apiKey); intent.putExtra(Constants.NOTIFICATION_TITLE, title); intent.putExtra(Constants.NOTIFICATION_MESSAGE, message); intent.putExtra(Constants.NOTIFICATION_URI, uri); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); /* notification.setLatestEventInfo(context, notficationMessage , message , contentIntent); notificationManager.notify(Consts.NOTIFICATION_ID, notification);*/ NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) .setSmallIcon(getNotificationIcon()).setContentTitle("").setContentText(""); Notification n = mBuilder.setContentIntent(contentIntent).setSmallIcon(getNotificationIcon()) .setTicker("").setWhen(System.currentTimeMillis()).setAutoCancel(true).setContentTitle(title) .setStyle(new NotificationCompat.BigTextStyle().bigText(message.replaceAll("[\t\n\r]", "\n"))) .setContentText(message).build(); if (isNotificationSoundEnabled()) { n.defaults |= Notification.DEFAULT_SOUND; } if (isNotificationVibrateEnabled()) { n.defaults |= Notification.DEFAULT_VIBRATE; } notificationManager.notify(Consts.NOTIFICATION_ID, n); // Notification } else { Log.w(LOGTAG, "Notificaitons disabled."); } }
From source file:com.classiqo.nativeandroid_32bitz.ui.PlaybackControlsFragment.java
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_playback_controls, container, false); mPlayPause = (ImageButton) rootView.findViewById(R.id.play_pause); mPlayPause.setEnabled(true);/*from w ww.j a v a2 s .co m*/ mPlayPause.setOnClickListener(mButtonListener); mTitle = (TextView) rootView.findViewById(R.id.title); mSubTitle = (TextView) rootView.findViewById(R.id.artist); mExtraInfo = (TextView) rootView.findViewById(R.id.extra_info); mAlbumArt = (ImageView) rootView.findViewById(R.id.album_art); rootView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getActivity(), FullScreenPlayerActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); MediaControllerCompat controller = ((FragmentActivity) getActivity()).getSupportMediaController(); MediaMetadataCompat metadata = controller.getMetadata(); if (metadata != null) { intent.putExtra(MusicPlayerActivity.EXTRA_CURRENT_MEDIA_DESCRIPTION, metadata.getDescription()); } startActivity(intent); } }); return rootView; }
From source file:com.nfc.gemkey.MainActivity.java
@Override public void onCreate(Bundle icicle) { if (DEBUG)/*from www . java 2s . co m*/ Log.d(TAG, "onCreate"); super.onCreate(icicle); setContentView(R.layout.activity_main); mAdapter = NfcAdapter.getDefaultAdapter(this); // Create a generic PendingIntent that will be deliver to this activity. The NFC stack // will fill in the intent with the details of the discovered tag before delivering to // this activity. mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); // Setup an intent filter for all MIME based dispatches IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); tagDetected.addCategory(Intent.CATEGORY_DEFAULT); mFilters = new IntentFilter[] { tagDetected }; mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE); mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED); registerReceiver(mUsbReceiver, filter); if (getLastNonConfigurationInstance() != null) { Log.i(TAG, "open usb accessory@onCreate"); mAccessory = (UsbAccessory) getLastNonConfigurationInstance(); openAccessory(mAccessory); } buttonLED = (ToggleButton) findViewById(R.id.nfc_btn); buttonLED.setBackgroundResource( buttonLED.isChecked() ? R.drawable.btn_toggle_yes : R.drawable.btn_toggle_no); buttonLED.setOnCheckedChangeListener(mKeyLockListener); tagId = (TextView) findViewById(R.id.nfc_tag); tagId.setText(R.string.nfc_scan_tag); // Avoid NetworkOnMainThreadException StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites() .detectNetwork().penaltyLog().build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects() .detectLeakedClosableObjects().penaltyLog().penaltyDeath().build()); setVolumeControlStream(AudioManager.STREAM_MUSIC); }
From source file:org.brightify.autoamplifier.AmplifierService.java
@Override public int onStartCommand(Intent intent, int flags, int startId) { initialiseAmplifier();//from w w w . j a v a2 s .com amplifyingThreadRunning = true; Thread amplifyingThread = new Thread(new Runnable() { @Override public void run() { while (amplifyingThreadRunning) { amplifier.amplify(); } } }); amplifyingThread.start(); NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext()); builder.setSmallIcon(R.drawable.ic_notification); builder.setContentTitle(getString(R.string.app_name)); builder.setContentText(getString(R.string.notification_title)); builder.setOngoing(true); builder.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity_.class).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0)); builder.addAction(R.drawable.ic_stat_disable, "", PendingIntent.getBroadcast(this, 0, new Intent(ACTION_DISABLE), 0)); builder.addAction(R.drawable.ic_stat_increase, "", PendingIntent.getBroadcast(this, 0, new Intent(ACTION_INCREASE), 0)); builder.addAction(R.drawable.ic_stat_decrease, "", PendingIntent.getBroadcast(this, 0, new Intent(ACTION_DECREASE), 0)); builder.setPriority(50); startForeground(NOTIFICATION_ID, builder.build()); return START_NOT_STICKY; }
From source file:com.packetsender.android.PacketListenerService.java
@Override protected void onHandleIntent(Intent intent) { dataStore = new DataStorage(getSharedPreferences(DataStorage.PREFS_SETTINGS_NAME, 0), getSharedPreferences(DataStorage.PREFS_SAVEDPACKETS_NAME, 0), getSharedPreferences(DataStorage.PREFS_SERVICELOG_NAME, 0), getSharedPreferences(DataStorage.PREFS_MAINTRAFFICLOG_NAME, 0)); listenportTCP = dataStore.getTCPPort(); listenportUDP = dataStore.getUDPPort(); Log.i("service", DataStorage.FILE_LINE("TCP: " + listenportTCP + " / UDP: " + listenportUDP)); Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0); startNotification();/*from w w w. jav a 2s.c o m*/ CharsetEncoder encoder = Charset.forName("US-ASCII").newEncoder(); ByteBuffer response = null; try { response = encoder.encode(CharBuffer.wrap("response")); } catch (CharacterCodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { SocketAddress localportTCP = new InetSocketAddress(listenportTCP); SocketAddress localportUDP = new InetSocketAddress(listenportUDP); tcpserver = ServerSocketChannel.open(); tcpserver.socket().bind(localportTCP); udpserver = DatagramChannel.open(); udpserver.socket().bind(localportUDP); tcpserver.configureBlocking(false); udpserver.configureBlocking(false); Selector selector = Selector.open(); tcpserver.register(selector, SelectionKey.OP_ACCEPT); udpserver.register(selector, SelectionKey.OP_READ); ByteBuffer receiveBuffer = ByteBuffer.allocate(1024); receiveBuffer.clear(); shutdownListener = new Runnable() { public void run() { if (false) { try { tcpserver.close(); } catch (IOException e) { } try { udpserver.close(); } catch (IOException e) { } stopSelf(); } else { mHandler.postDelayed(shutdownListener, 2000); } } }; sendListener = new Runnable() { public void run() { //Packet fetchedPacket = mDbHelper.needSendPacket(); Packet[] fetchedPackets = dataStore.fetchAllServicePackets(); if (fetchedPackets.length > 0) { dataStore.clearServicePackets(); Log.d("service", DataStorage.FILE_LINE("sendListener found " + fetchedPackets.length + " packets")); for (int i = 0; i < fetchedPackets.length; i++) { Packet fetchedPacket = fetchedPackets[i]; Log.d("service", DataStorage.FILE_LINE("send packet " + fetchedPacket.toString())); } new SendPacketsTask().execute(fetchedPackets); } mHandler.postDelayed(sendListener, 2000); } }; //start shutdown listener mHandler.postDelayed(shutdownListener, 2000); //start send listener mHandler.postDelayed(sendListener, 5000); while (true) { try { // Handle per-connection problems below // Wait for a client to connect Log.d("service", DataStorage.FILE_LINE("waiting for connection")); selector.select(); Log.d("service", DataStorage.FILE_LINE("client connection")); Set keys = selector.selectedKeys(); for (Iterator i = keys.iterator(); i.hasNext();) { SelectionKey key = (SelectionKey) i.next(); i.remove(); Channel c = (Channel) key.channel(); if (key.isAcceptable() && c == tcpserver) { SocketChannel client = tcpserver.accept(); if (client != null) { Socket tcpSocket = client.socket(); packetCounter++; DataInputStream in = new DataInputStream(tcpSocket.getInputStream()); byte[] buffer = new byte[1024]; int received = in.read(buffer); byte[] bufferConvert = new byte[received]; System.arraycopy(buffer, 0, bufferConvert, 0, bufferConvert.length); Packet storepacket = new Packet(); storepacket.tcpOrUdp = "TCP"; storepacket.fromIP = tcpSocket.getInetAddress().getHostAddress(); storepacket.toIP = "You"; storepacket.fromPort = tcpSocket.getPort(); storepacket.port = tcpSocket.getLocalPort(); storepacket.data = bufferConvert; UpdateNotification("TCP:" + storepacket.toAscii(), "From " + storepacket.fromIP); Log.i("service", DataStorage.FILE_LINE("Got TCP")); //dataStore.SavePacket(storepacket); /* Intent tcpIntent = new Intent(); tcpIntent.setAction(ResponseReceiver.ACTION_RESP); tcpIntent.addCategory(Intent.CATEGORY_DEFAULT); tcpIntent.putExtra(PARAM_OUT_MSG, storepacket.name); sendBroadcast(tcpIntent); */ storepacket.nowMe(); dataStore.saveTrafficPacket(storepacket); Log.d("service", DataStorage.FILE_LINE("sendBroadcast")); if (false) //mDbHelper.getSettings(PSDbAdapter.KEY_SETTINGS_SENDRESPONSE).equalsIgnoreCase("Yes")) { storepacket = new Packet(); storepacket.name = dataStore.currentTimeStamp(); ; storepacket.tcpOrUdp = "TCP"; storepacket.fromIP = "You"; storepacket.toIP = tcpSocket.getInetAddress().getHostAddress(); storepacket.fromPort = tcpSocket.getLocalPort(); storepacket.port = tcpSocket.getPort(); // storepacket.data = Packet.toBytes(mDbHelper.getSettings(PSDbAdapter.KEY_SETTINGS_SENDRESPONSETEXT)); storepacket.nowMe(); dataStore.saveTrafficPacket(storepacket); Log.d("service", DataStorage.FILE_LINE("sendBroadcast")); client.write(response); // send response } client.close(); // close connection } } else if (key.isReadable() && c == udpserver) { DatagramSocket udpSocket; DatagramPacket udpPacket; byte[] buffer = new byte[2048]; // Create a packet to receive data into the buffer udpPacket = new DatagramPacket(buffer, buffer.length); udpSocket = udpserver.socket(); receiveBuffer.clear(); InetSocketAddress clientAddress = (InetSocketAddress) udpserver.receive(receiveBuffer); if (clientAddress != null) { String fromAddress = clientAddress.getAddress().getHostAddress(); packetCounter++; int received = receiveBuffer.position(); byte[] bufferConvert = new byte[received]; System.arraycopy(receiveBuffer.array(), 0, bufferConvert, 0, bufferConvert.length); Packet storepacket = new Packet(); storepacket.tcpOrUdp = "UDP"; storepacket.fromIP = clientAddress.getAddress().getHostAddress(); storepacket.toIP = "You"; storepacket.fromPort = clientAddress.getPort(); storepacket.port = udpSocket.getLocalPort(); storepacket.data = bufferConvert; UpdateNotification("UDP:" + storepacket.toAscii(), "From " + storepacket.fromIP); //dataStore.SavePacket(storepacket); storepacket.nowMe(); dataStore.saveTrafficPacket(storepacket); Log.d("service", DataStorage.FILE_LINE("sendBroadcast")); if (false)//mDbHelper.getSettings(PSDbAdapter.KEY_SETTINGS_SENDRESPONSE).trim().equalsIgnoreCase("Yes")) { storepacket = new Packet(); storepacket.name = dataStore.currentTimeStamp(); ; storepacket.tcpOrUdp = "UDP"; storepacket.fromIP = "You"; storepacket.toIP = clientAddress.getAddress().getHostAddress(); storepacket.fromPort = udpSocket.getLocalPort(); storepacket.port = clientAddress.getPort(); // storepacket.data = Packet.toBytes(mDbHelper.getSettings(PSDbAdapter.KEY_SETTINGS_SENDRESPONSETEXT)); //dataStore.SavePacket(storepacket); udpserver.send(response, clientAddress); storepacket.nowMe(); dataStore.saveTrafficPacket(storepacket); Log.d("service", DataStorage.FILE_LINE("sendBroadcast")); } } } } } catch (java.io.IOException e) { Log.i("service", DataStorage.FILE_LINE("IOException ")); } catch (Exception e) { Log.w("service", DataStorage.FILE_LINE("Fatal Error: " + Log.getStackTraceString(e))); } } } catch (BindException e) { //mDbHelper.putServiceError("Error binding to port"); dataStore.putToast("Port already in use."); Log.w("service", DataStorage.FILE_LINE("Bind Exception: " + Log.getStackTraceString(e))); } catch (Exception e) { //mDbHelper.putServiceError("Fatal Error starting service"); Log.w("service", DataStorage.FILE_LINE("Startup error: " + Log.getStackTraceString(e))); } stopNotification(); }