TransactionTooLargeException while notify notification

跟風遠走 提交于 2019-12-12 23:36:45

问题


I have update time while recording in android, I using CountDownTimer and update to remote view on notification. I have optimized smallest data but still get TransactionTooLargeException.

public void showNotificationRecording() {
    mRemoteViews = new RemoteViews(getPackageName(), R.layout.notify_recording_layout);
    mBuilder = new NotificationCompat.Builder(mContext);
    isSecretModeEnable = mSharePreferencesSettings.getBoolean(RecordSettings.SECRET_MODE, false);
    Log.d(TAG, "isSecretModeEnable " + isSecretModeEnable);
    if (isSecretModeEnable) {
        mRemoteViews = new RemoteViews(getPackageName(), R.layout.notify_secret_mode_layout);
        mBuilder.setPriority(Notification.PRIORITY_MIN)
                .setSmallIcon(R.drawable.ic_notify_secret)
                .setContent(mRemoteViews);

        // Save File Notification
        Intent mIntentSave = new Intent(mContext, RecordAudioService.class);
        mIntentSave.setAction(ACTION_STOP_RECORD);
        PendingIntent mSavePendingIntent = PendingIntent.getService(mContext, 0, mIntentSave, 0);
        mRemoteViews.setOnClickPendingIntent(R.id.iv_notify_save, mSavePendingIntent);
    } else {
        mRemoteViews = new RemoteViews(getPackageName(), R.layout.notify_recording_layout);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
        }
        mBuilder.setSmallIcon(R.drawable.record_circle)
                .setPriority(Notification.PRIORITY_MAX)
                .setDefaults(Notification.DEFAULT_LIGHTS)
                .setContent(mRemoteViews);

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent mMainPendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(mMainPendingIntent);

        mRemoteViews.setTextViewText(R.id.tv_notify_time, mRecordTimeNotification);
        // Pause and Resume Notification
        Intent mIntentPauseAndResume = new Intent(mContext, RecordAudioService.class);
        mIntentPauseAndResume.setAction(ACTION_PAUSE_AND_RESUME);
        PendingIntent mPauseAndResumePending = PendingIntent.getService(mContext, 0, mIntentPauseAndResume, 0);
        mRemoteViews.setOnClickPendingIntent(R.id.iv_notify_pause_and_resume, mPauseAndResumePending);
        // Save File Notification
        Intent mIntentSave = new Intent(mContext, RecordAudioService.class);
        mIntentSave.setAction(ACTION_STOP_RECORD);
        PendingIntent mSavePendingIntent = PendingIntent.getService(mContext, 0, mIntentSave, 0);
        mRemoteViews.setOnClickPendingIntent(R.id.iv_notify_save, mSavePendingIntent);
        //Delete Notification
        Intent mIntentDelete = new Intent(mContext, RecordAudioService.class);
        mIntentDelete.setAction(ACTION_NOTIFICATION_DISCARD);
        PendingIntent mDeletePendingIntent = PendingIntent.getService(mContext, 0, mIntentDelete, 0);
        mRemoteViews.setOnClickPendingIntent(R.id.iv_notify_closed, mDeletePendingIntent);

        if (isRecording) {
            mRemoteViews.setImageViewResource(R.id.iv_notify_pause_and_resume, R.drawable.ic_notify_pause);
            mState = mContext.getString(R.string.recording_state);
            mRemoteViews.setTextViewText(R.id.tv_notify_status, mState);
        } else {
            mRemoteViews.setImageViewResource(R.id.iv_notify_pause_and_resume, R.drawable.ic_notify_record);
            mState = mContext.getString(R.string.pause_state);
            mRemoteViews.setTextViewText(R.id.tv_notify_status, mState);
        }
    }

    isShowNotification = true;
    mNotificationManager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
    startForeground(NOTIFICATION_ID, mBuilder.build());
}
public CountDownTimer countDownRecordingTime() {
    CountDownTimer time;
    time = new CountDownTimer(Long.MAX_VALUE, 250) {

        @Override
        public void onTick(long millisUntilFinished) {
            final Integer convertToSecond = mCountTimer / 4;
            if (mTime != convertToSecond) {
                mTime = convertToSecond;
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        showTimeRecording(mTime);
                    }
                });
            }
            mCountTimer++;

            // check when user setting timer
            if (mTimerToEnd != 0 && mTimerToEnd == convertToSecond) {
                onStopRecording();
            }
        }

        @Override
        public void onFinish() {

        }
    };
    return time;
}

And I update notification as below, I just only update time recording to notification.

private void showTimeRecording(int convertToSecond) {
    int hours = convertToSecond / 3600;
    int minutes = (convertToSecond % 3600) / 60;
    int seconds = convertToSecond % 60;
    mRecordTime = String.format(Locale.getDefault(), "%02d:%02d:%02d", hours, minutes, seconds);
    mRecordTimeNotification = mRecordTime;

    mRemoteViews.setTextViewText(R.id.tv_notify_time, mRecordTimeNotification);
    if (!mState.equals(mContext.getString(R.string.recording_state))) {
        mState = mContext.getString(R.string.recording_state);
        mRemoteViews.setTextViewText(R.id.tv_notify_status, mState);
    }
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

Please help me to solve this problem.

来源:https://stackoverflow.com/questions/48284000/transactiontoolargeexception-while-notify-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!