我想用图片创建一个实时的cpu(和/或)ram通知,但我不知道如何将图片与从系统中获取的值链接起来。
示例图像:
对于空闲-->http://imageshack.us/photo/my-images/840/idleq.png/
10%至24%--->http://imageshack.us/photo/my-images/841/18037282.png/
25%至49%-->http://imageshack.us/photo/my-images/267/74169930.png/
50%至79%--->http://imageshack.us/photo/my-images/692/63885257.png/
对于80到99%--->http://imageshack.us/photo/my-images/20/39599298.png/
100%--->http://imageshack.us/photo/my-images/715/100to.png/
很抱歉使用imageshack.us,但我还不能上传图片。
非常感谢。
我不认为你可以更改通知上的图片,但你可以创建多个通知,并根据自己的意愿调用它们。也许有更好的答案,但这就足够了。
创建和销毁通知的方法
private NotificationManager notificationManager;
/* Params:
* int imageResourceId: The resource image to set
* String titleText: the title of the notification
* String statusText: the subtitle of the notification
*/
public void createNotification(int imageResourceId, String titleText, String statusText)
{
Notification notification = new Notification(imageResourceId, titleText, System.currentTimeMillis());
notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.setLatestEventInfo(mContext, titleText, statusText, null);
notificationManager.notify(1, notification);
}
/* Destroys all notifications */
public void destroyNotification()
{
if (notificationManager != null)
notificationManager.cancelAll();
}
好的,这包括创建和删除通知,如何根据不同的电池百分比进行更改?很简单,这取决于您的图像资源。
使用示例
if (batteryPercentage > 85)
{
destroyNotification();
createNotification(R.drawable.highBatteryIcon, "Good Battery", "85-100%");
}
else if (batteryPercentage <= 85)
{
destroyNotification();
createNotification(R.drawable.lowBatteryIcon, "OK Battery", "0-85%");
}
把你所有的图片放在你的可绘制文件夹中,并根据需要修改你所有百分比的代码,等等。但这就是我个人的做法。
此外,您可以通过更新通知的latestEventInfo并重新通知notificationManager来显示动态百分比等内容,但您可以玩得很开心;)