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
Image 2
Notification that is not removable by user:
Image 3
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.
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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.