我正在尝试使用通过字符串创建位图的函数为通知创建动态图标。
开始我的项目时,我想使用API级别22,但是我意识到 public Notification.Builder setSmallIcon(图标图标)在API 23之前不可用。 所以我更改了MinSDKVersion,但仍然出现不兼容的类型错误...
package com.example.netmetah;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;
import android.app.Notification.Builder;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class NetMonitorService extends IntentService {
public NetMonitorService() {
super("NetMeterListening");
}
@Override
protected void onHandleIntent(Intent workIntent) {
Context context = getApplicationContext();
CharSequence text = "Le service marche";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
createNotification(createBitmapFromString("23","kb"));
}
private Bitmap createBitmapFromString(String speed, String units) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setTextSize(55);
paint.setTextAlign(Paint.Align.CENTER);
Paint unitsPaint = new Paint();
unitsPaint.setAntiAlias(true);
unitsPaint.setTextSize(40);
unitsPaint.setTextAlign(Paint.Align.CENTER);
Rect textBounds = new Rect();
paint.getTextBounds(speed, 0, speed.length(), textBounds);
Rect unitsTextBounds = new Rect();
unitsPaint.getTextBounds(units, 0, units.length(), unitsTextBounds);
int width = (textBounds.width() > unitsTextBounds.width()) ? textBounds.width() : unitsTextBounds.width();
Bitmap bitmap = Bitmap.createBitmap(width + 10, 90,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawText(speed, width / 2 + 5, 50, paint);
canvas.drawText(units, width / 2, 90, unitsPaint);
return bitmap;
}
public static long[] readProc() {
BufferedReader br;
String line;
String[] lines;
long rx = 0;
long tx = 0;
try {
br = new BufferedReader(new FileReader("/proc/net/dev"));
while ((line = br.readLine()) != null) {
if (line.contains("eth") || line.contains("wlan")) {
lines = line.trim().split("\\s+");
rx += Long.parseLong(lines[1]);
tx += Long.parseLong(lines[9]);
}
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
long[] bytes = {rx, tx};
return bytes;
}
private void createNotification(Bitmap icon) {
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationIntent.setData(Uri.parse("http://www.google.ca"));
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, "fuck")
.setCategory(Notification.CATEGORY_PROMO)
.setSmallIcon(Icon.createWithBitmap(icon))
.setContentTitle("C'est quoi un titre")
.setContentText("C'est quoi un text")
.setAutoCancel(true)
.setVisibility(1) //Public
.addAction(android.R.drawable.ic_menu_view, "View details", contentIntent)
.setContentIntent(contentIntent)
.setPriority(Notification.PRIORITY_HIGH)
.setChannelId("fuck")
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}).build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(123, notification);
}
}
答案 0 :(得分:0)
您正在使用NotificationCompat.Builder
。根据{{3}},setSmallIcon方法采用可绘制的资源ID。
很容易将其与Notification.Builder
的方法混淆,其中一种方法具有可绘制的资源ID,而另一种方法则具有Icon
。