【问题标题】:Android: Notification stops updating once it has updated for a certain number of times on Kit-KatAndroid:通知在 Kit-Kat 上更新一定次数后停止更新
【发布时间】:2016-08-23 13:54:18
【问题描述】:

所以我的应用中有一个通知。通知有一些文本和一个图像,我从一开始填充的 LruCache 中获取。问题是,在我的情况下,在 Kit-Kat 设备(Moto E,XIomi)上,如果通知已经更新了固定次数,则通知停止更新,比如说 n(这个 n 通常是固定的对于每个设备,并且

            String title = CommonUtils.getTitleFromID(id, context);
            Bitmap bitmap = DataProvider.getInstance(context).diskLruImageCache.getBitmap(id + "");
            if (playbackPaused) {
                bigView.setImageViewResource(R.id.pause, R.drawable.pause_noti);
                smallView.setImageViewResource(R.id.pause1, R.drawable.pause_noti);
                remoteViews.setImageViewResource(R.id.pause2, R.drawable.pause_noti);
                playbackPaused = false;
                Log.d(TAG, "noti-false");
            }

            bigView.setTextViewText(R.id.title, title + "");
            bigView.setImageViewBitmap(R.id.img, bitmap);

            smallView.setTextViewText(R.id.title1, title);
            smallView.setImageViewBitmap(R.id.img1, bitmap);

            mNotificationManager.notify(NOTIFY_ID, notification);

我尝试了一些调试,我发现如果我不设置位图,(只做
bitmap = null
那么一切正常。

我不明白为什么会这样。这个问题是特定于 android 版本的,因为我已经在 Nexus 5 (Lollipop) 和其他 Android 5 手机上进行了测试,但没有发生这种情况。有人知道这背后的原因是什么吗?

我并不期待确切的情况。即使是一些正确方向的想法也会很有帮助。谢谢!!

我还在为我的 DiskLruCache 添加代码以备不时之需,

public class DiskLruImageCache {

    private DiskLruCache mDiskCache;
    private Bitmap.CompressFormat mCompressFormat = Bitmap.CompressFormat.JPEG;
    private int mCompressQuality = 70;
    private static final int APP_VERSION = 1;
    private static final int VALUE_COUNT = 1;
    private static final String TAG = "DiskLruImageCache";

    public DiskLruImageCache( Context context,String uniqueName, int diskCacheSize,
                              Bitmap.CompressFormat compressFormat, int quality ) {
        try {
            final File diskCacheDir = getDiskCacheDir(context, uniqueName );
            mDiskCache = DiskLruCache.open( diskCacheDir, APP_VERSION, VALUE_COUNT, diskCacheSize );
            mCompressFormat = compressFormat;
            mCompressQuality = quality;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private boolean writeBitmapToFile( Bitmap bitmap, DiskLruCache.Editor editor )
            throws IOException {
        OutputStream out = null;
        try {
            out = new BufferedOutputStream( editor.newOutputStream( 0 ), Utils.IO_BUFFER_SIZE );
            return bitmap.compress( mCompressFormat, mCompressQuality, out );
        } finally {
            if ( out != null ) {
                out.close();
            }
        }
    }


    private boolean writeUriToFile( Uri bitmap, DiskLruCache.Editor editor )
            throws IOException, FileNotFoundException {
        OutputStream out = null;
        try {
            out = new BufferedOutputStream( editor.newOutputStream( 0 ), Utils.IO_BUFFER_SIZE );
            return true;
        } finally {
            if ( out != null ) {
                out.close();
            }
        }
    }

    private File getDiskCacheDir(Context context, String uniqueName) {

        // Check if media is mounted or storage is built-in, if so, try and use external cache dir
        // otherwise use internal cache dir
        final String cachePath =
                Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||
                        !Utils.isExternalStorageRemovable() ?
                        Utils.getExternalCacheDir(context).getPath() :
                        context.getCacheDir().getPath();

        return new File(cachePath + File.separator + uniqueName);
    }

    public void put( String key, Bitmap data ) {

        DiskLruCache.Editor editor = null;
        try {
            editor = mDiskCache.edit( key );
            if ( editor == null ) {
                return;
            }

            if( writeBitmapToFile( data, editor ) ) {
                mDiskCache.flush();
                editor.commit();
                if ( BuildConfig.DEBUG ) {
                    Log.d( "cache_test_DISK_", "image put on disk cache " + key );
                }
            } else {
                editor.abort();
                if ( BuildConfig.DEBUG ) {
                    Log.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + key );
                }
            }
        } catch (IOException e) {
            if ( BuildConfig.DEBUG ) {
                Log.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + key );
            }
            try {
                if ( editor != null ) {
                    editor.abort();
                }
            } catch (IOException ignored) {
            }
        }

    }

    public void put( String key, Uri data ) {

        DiskLruCache.Editor editor = null;
        try {
            editor = mDiskCache.edit( key );
            if ( editor == null ) {
                return;
            }

            if( writeUriToFile( data, editor ) ) {
                mDiskCache.flush();
                editor.commit();
                if ( BuildConfig.DEBUG ) {
                    Log.d( "cache_test_DISK_", "image put on disk cache " + key );
                }
            } else {
                editor.abort();
                if ( BuildConfig.DEBUG ) {
                    Log.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + key );
                }
            }
        } catch (IOException e) {
            if ( BuildConfig.DEBUG ) {
                Log.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + key );
            }
            try {
                if ( editor != null ) {
                    editor.abort();
                }
            } catch (IOException ignored) {
            }
        }

    }

    public synchronized Bitmap getBitmap( String key ) {

        Bitmap bitmap = null;
        DiskLruCache.Snapshot snapshot = null;
        try {

            snapshot = mDiskCache.get( key );
            if ( snapshot == null ) {
                return null;
            }
            final InputStream in = snapshot.getInputStream( 0 );
            if ( in != null ) {
                final BufferedInputStream buffIn =
                        new BufferedInputStream( in, Utils.IO_BUFFER_SIZE );
                bitmap = BitmapFactory.decodeStream(buffIn);
            }
        } catch ( IOException e ) {
            e.printStackTrace();
        } finally {
            if ( snapshot != null ) {
                snapshot.close();
            }
        }

        if ( BuildConfig.DEBUG ) {
            Log.d( "cache_test_DISK_", bitmap == null ? "" : "image read from disk " + key);
        }

        return bitmap;

    }

    public boolean containsKey( String key ) {

        boolean contained = false;
        DiskLruCache.Snapshot snapshot = null;
        try {
            snapshot = mDiskCache.get( key );
            contained = snapshot != null;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if ( snapshot != null ) {
                snapshot.close();
            }
        }

        return contained;

    }

    public boolean removeKey( String key ) {

        boolean removed=false;
        DiskLruCache.Snapshot snapshot = null;
        try {
            removed = mDiskCache.remove( key );
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if ( snapshot != null ) {
                snapshot.close();
            }
        }
        return removed;
    }

    public void clearCache() {
        if ( BuildConfig.DEBUG ) {
            Log.d("cache_test_DISK_", "disk cache CLEARED");
        }
        try {
            mDiskCache.delete();
        } catch ( IOException e ) {
            e.printStackTrace();
        }
    }

    public File getCacheFolder() {
        return mDiskCache.getDirectory();
    }

}

【问题讨论】:

  • 只是对您的代码的观察和可能的原因:您没有释放位图,它可能会因表面内存而崩溃。你有关于这个问题的任何痕迹吗??
  • @eduyayo 嘿,谢谢您的回复,我没有注意到任何明显的痕迹。但是我应该在哪里以及如何释放位图,这个问题对我来说真的很重要,但我无法解决它。你能建议改变吗?谢谢!!
  • 在“使用”之后,您应该致电bitmap.recycle()。我建议您在设置新的之前保留对以前使用的参考并回收...
  • 顺便说一句! writeBitmapToFile当你预处理并保存任何位图时,千万不要在没有回收的情况下丢失东西!!
  • @eduyayo 如果我回收位图,我会收到错误无法在 mNotificationManager.notify(NOTIFY_ID,musicNotification); 行打包回收的位图; :( 当我试图解决这个问题时,我也遇到了这个错误。

标签: android android-notifications


【解决方案1】:

如果 kitkat 和更低版本的条件 ... 因为 kitkat 版本在通知系统中的颜色发生变化.. 我的 mipmap 通知 img siz 是 96*96

  NOTIFICATION_ID= Integer.parseInt(arr_msg[7].toString());
mBuilder.setSmallIcon(R.mipmap.alert_nodification_icon)
        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.alert_nodification_icon))
        .setContentTitle(notification_vehicle_no)
        .setStyle(new NotificationCompat.BigTextStyle()
                .bigText(notification_msg))
        .setContentText(notification_msg);}
    mBuilder.setContentIntent(contentIntent);
    Notification notification = mBuilder.build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    mNotificationManager.notify(NOTIFICATION_ID,notification);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    • 2021-02-03
    • 2013-03-29
    • 1970-01-01
    • 2011-10-05
    相关资源
    最近更新 更多