Android:通知会在Kit-Kat上更新一定次数后停止更新 [英] Android: Notification stops updating once it has updated for a certain number of times on Kit-Kat

查看:60
本文介绍了Android:通知会在Kit-Kat上更新一定次数后停止更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此我的应用程序中有一条通知.该通知包含一些文本和图像,这些文本和图像是从我首先填充的LruCache中获取的.问题是在我的情况下,在 Kit-Kat设备(Moto E,XIomi)上,通知已停止更新(如果它已经更新了固定的次数),比如说n(此n通常是固定的对于每个设备,则< 10).这是我的更新代码

So I have a notification in my app. The notification has some text and an image which I fetch from a LruCache which I populate at the start. The problem is that on Kit-Kat devices (Moto E, XIomi) in my case, the notification stops updating if it has already updated a fixed number of times, lets say n (this n is usually fixed for each device, and is <10). This is my code for updating

            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)
,然后一切正常.

I tried with a bit of debugging and all and I found that if I do not set the bitmap, (and just do
bitmap = null)
then everything works fine.

我不明白为什么会这样.由于我已经在Nexus 5(Lollipop)和其他Android 5手机上进行过测试,因此该问题仅适用于android版本,但这种情况不会发生.有人知道背后的原因是什么吗?

I don't understand why this is happening. This issue is android version specific since I already tested on Nexus 5 (Lollipop) and other Android 5 phones but there this doesn't happen. Does someone have any idea what the reason behind this might be?

我不希望有确切的情况.甚至正确方向上的一些想法也将非常有帮助.谢谢!!

I am not expecting an exact situation. Even some ideas in the right direction will be very helpful. Thanks !!

我还在为我的DiskLruCache添加代码,以防可能需要,

I am also adding the code for my DiskLruCache in case that might be needed,

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

}

推荐答案

是否输入kitkat和更低版本的条件...在通知系统中kitkat版本更改颜色后,becoz会发出警告..我的mipmap通知img siz为96 * 96

put if condition for kitkat and lower versions ... becoz after kitkat version changes in color in notification system.. my mipmap notify img siz is 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);

这篇关于Android:通知会在Kit-Kat上更新一定次数后停止更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆