首页 > 解决方案 > 不兼容的类型:图标无法转换为 int

问题描述

我正在尝试使用从字符串创建位图的函数为我的通知创建一个动态图标。

当我开始我的项目时,我想使用 API 级别 22,但我意识到公共 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);

    }
}

标签: androidbitmapiconsandroid-notifications

解决方案


您正在使用NotificationCompat.Builder. 根据文档, setSmallIcon 方法采用可绘制资源 ID。

很容易将其与Notification.Builder一个采用可绘制资源 ID 而另一个采用Icon.


推荐阅读