Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 不兼容类型:无法将图标转换为int_Android_Bitmap_Icons_Android Notifications - Fatal编程技术网

Android 不兼容类型:无法将图标转换为int

Android 不兼容类型:无法将图标转换为int,android,bitmap,icons,android-notifications,Android,Bitmap,Icons,Android Notifications,我正在尝试使用从字符串创建位图的函数为通知创建动态图标 当我开始我的项目时,我想使用API级别22,但我意识到,public Notification.Builder setSmallIcon(图标图标)直到API 23才可用。 所以我改变了我的Minsdk版本,但我仍然得到不兼容类型错误 package com.example.netmetah; import android.app.IntentService; import android.app.Notification; import

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

当我开始我的项目时,我想使用API级别22,但我意识到,public Notification.Builder setSmallIcon(图标图标)直到API 23才可用。 所以我改变了我的Minsdk版本,但我仍然得到不兼容类型错误

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);

    }
}

您正在使用
NotificationCompat.Builder
。根据setSmallIcon方法获取可绘制的资源ID


这很容易与
通知的方法相混淆。Builder
其中一个使用可绘制的资源ID,另一个使用
图标

可能的重复-不,它没有回答我的问题,事实上,我最初是使用此帖子作为指南,在代码中创建此工作组的,您是否以位图形式显示图标,该图标是否来自服务器?我使用**私有位图createBitmapFromString(字符串速度,字符串单位)**从字符串生成它*创建的位图与setLargeIcon()一起工作,但与SetMallIcon()不一起工作,就像我仍然在API 22上一样,我无法使用“新”位图来自NotificationCompat.builder的重载函数在开发者博客的通知部分,它提到了如果您想使用setSmallIcon(),那么您需要传递R.drawable.image,因为它存在于您的包中,那么大图标有什么问题吗?谢谢,这正是我想要的:)