我希望在Android状态栏中实现一个显示网络速度的通知,而且希望这个通知不可由用户移除,只能由应用程序本身移除。
我查看了
我知道实现此功能是可能的,但我不知道如何实现它。
我找到了一个很好地实现了这一功能的应用程序,它的名字叫做internet speed meter lite。
正如您所知,通过
我放了一些图片以便更好地理解。
Android状态栏中的网络速度:
Image 1
![Internet speed in status bar of android![][1]](https://istack.dev59.com/pzjk5.webp)
Image 2
![enter image description here](https://istack.dev59.com/5eWUu.webp)
不可被用户移除的通知:
Image 3
![Notification that is not removable by user](https://istack.dev59.com/1A6LU.webp)
以下是使用上述类通知用户的代码示例:
每秒钟调用以上代码。
我查看了
NotificationCompat.Builder
API,但是无法找到设置状态栏文本并实时更新的相关API。我知道实现此功能是可能的,但我不知道如何实现它。
我找到了一个很好地实现了这一功能的应用程序,它的名字叫做internet speed meter lite。
正如您所知,通过
NotificationCompat.Builder
的setSmallIcon
无法实现此功能。我放了一些图片以便更好地理解。
Android状态栏中的网络速度:
Image 1
![Internet speed in status bar of android![][1]](https://istack.dev59.com/pzjk5.webp)
Image 2
![enter image description here](https://istack.dev59.com/5eWUu.webp)
不可被用户移除的通知:
Image 3
![Notification that is not removable by user](https://istack.dev59.com/1A6LU.webp)
更新:
这是我的通知代码,但它没有按照我的期望执行。
我在通知中使用滚动文本来向用户显示速度,但它没有按照我的期望执行。
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);
}
}
以下是使用上述类通知用户的代码示例:
downloadSpeedNotification.setSpeed(Formatter.humanReadableByteCount(rx, true));
downloadSpeedNotification.alert();
每秒钟调用以上代码。