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.