4

I want to implement a notification that shows internet speed in status bar of android also I want that this notification not removable by user and only removable by application itself.
I'd looked at NotificationCompat.Builder Api but I could not find any Api for setting text in status bar that updates regularly.
I know that it is possible to implementing this feature but I don't know how to implement it.
I found an app that implements it very well it's name is internet speed meter lite.
As you know this feature could not be implemented by setSmallIcon of NotificationCompat.Builder.
I put images for better understanding.
Internet speed in status bar of android:
Image 1
Internet speed in status bar of android![][1]
Image 2
enter image description here
Notification that is not removable by user:
Image 3
Notification that is not removable by user

Update:
This is my code for notification but it did not action as I want.
I used ticker text in my notification for showing speed to user but it did not action as I want.

public class DownloadSpeedNotification {

private NotificationCompat.Builder mBuilder;
private Context mContext;
private static final int NOTIFICATION_ID = 2;

public DownloadSpeedNotification(Context context) {
    mContext = context;
    mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.notification_icon).
                    setContentText("Download meter content text").
                    setContentTitle("Download meter content title");

    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(pendingIntent);
}

public void alert() {
    // Gets an instance of the NotificationManager service
    NotificationManager mNotifyMgr =
            (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    // Builds the notification and issues it.
    mNotifyMgr.notify(NOTIFICATION_ID, mBuilder.build());
}

public void setSpeed(String speed) {
    mBuilder.setTicker(speed);
}
}

And this is code that use above class to notify user:

downloadSpeedNotification.setSpeed(Formatter.humanReadableByteCount(rx, true));
        downloadSpeedNotification.alert();

Above code called every 1 second.

taher
  • 539
  • 1
  • 9
  • 22

2 Answers2

0

Did you tried to work with NotificationManager? http://developer.android.com/reference/android/app/NotificationManager.html

There's a notify method, you can call it everytime when you need for example from service, which will be looking for your connection and tell how to change it.

EDIT 1 Google even gives a good example, how to work with notification manager http://developer.android.com/training/notify-user/managing.html

k.nadonenko
  • 628
  • 2
  • 7
  • 23
  • My mean is notification status bar text as shown in images attached to question not notification text. In other words I want to show text instead of icon of notification in status bar. This text will shows internet speed. Please look at image 1 and 2 in question. – taher Apr 20 '15 at 19:06
  • @taher maybe this would help? http://stackoverflow.com/questions/19610598/android-adding-text-from-textview-to-status-bar – k.nadonenko Apr 21 '15 at 07:07
0

Try this..

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(ns);
long when = System.currentTimeMillis();

Notification notification = new Notification("your icon", "your ticker text", when);
/*<set your intents here>*/
mNotificationManager.notify(notificationID, notification);

Replace the notificationID with an integer. As long as you are sending a new notification with the same notificationID, it will replace the old one.

Lal
  • 14,726
  • 4
  • 45
  • 70
  • My mean is notification status bar text as shown in images attached to question not notification text. In other words I want to show text instead of icon of notification in status bar. This text will shows internet speed. Please look at image 1 and 2 in question. – taher Apr 20 '15 at 19:06
  • Also I should say that I tested ticker text in my notification but it did not action as I want. – taher Apr 20 '15 at 19:08